首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过app登录到我的大学生门户

通过app登录到我的大学生门户
EN

Stack Overflow用户
提问于 2015-08-04 21:24:01
回答 3查看 662关注 0票数 1

我想用我的应用程序连接到我所在大学的学生门户网站,并从网站上获取某些信息,比如当前的结果。因此,到目前为止,我已经编写了这段代码,我在登录时遇到了困难。每当我按下登录按钮,应用程序就会崩溃。我知道我的代码有问题,所以如果你们帮我解决这个问题,我会非常感激的。这是我的学生档案的网址。

http://111.68.99.8/StudentProfile/

这是我迄今所写的代码..。

java文件

代码语言:javascript
运行
复制
package com.example.ebad.testing;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity {

    Button login;
    TextView Enrollement,password,E;



    private static final String TAG = "MyActivity";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Enrollement = (TextView) findViewById(R.id.Enrollment);
        password = (TextView) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login_button);
        E = (TextView) findViewById(R.id.message);

        login.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {

                        String GMAIL_CONTACTS = "http://111.68.99.8/StudentProfile/PersonalInfo.aspx";
                        String GMAIL_LOGIN = "http://111.68.99.8/StudentProfile/";
                        String message_e = E.toString();
                        message_e += "";

                        String Enrollement_e = Enrollement.toString();
                        String password_e  = password.toString();

                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpPost httpPost = new HttpPost(GMAIL_LOGIN);

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                        nameValuePairs.add(new BasicNameValuePair("ctl00_Body_ENROLLMENTTextBox_tb", Enrollement_e));
                        nameValuePairs.add(new BasicNameValuePair("ctl00_Body_PasswordTextBox_tb", password_e));
                        nameValuePairs.add(new BasicNameValuePair("ctl00_Body_LoginButton", "login"));

                        try {
                            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                        // Execute HTTP Post Request
                        HttpResponse response = null;
                        try {
                            response = httpClient.execute(httpPost);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        message_e +=response.getStatusLine();
                        E.setText(message_e);

                        Log.d(TAG, "response stat code " + response.getStatusLine().getStatusCode());

                        if (response.getStatusLine().getStatusCode() < 400) {

                            String cookie = response.getFirstHeader("Set-Cookie")
                                    .getValue();
                            Log.d(TAG, "cookie: " + cookie);

                            // get the contacts page
                            HttpGet getContacts = new HttpGet(GMAIL_CONTACTS);
                            getContacts.setHeader("Cookie", cookie);
                            try {
                                response = httpClient.execute(getContacts);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            InputStream ins = null;
                            try {
                                ins = response.getEntity().getContent();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            BufferedReader in = new BufferedReader(new InputStreamReader(
                                    ins));

                            String inputLine;
                            try {
                                while ((inputLine = in.readLine()) != null) {
                                    Log.d(TAG, " " + inputLine);
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            try {
                                in.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {
                            Log.d(TAG, "Response error: "
                                    + response.getStatusLine().getStatusCode());
                        }


                    }
                }
        );


    }








    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是xml文件..。

代码语言:javascript
运行
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter the Enrollement"
        android:id="@+id/Enrollment"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="51dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/password"
        android:hint="Enter the Password"
        android:layout_marginTop="50dp"
        android:layout_below="@+id/Enrollment"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LOGIN"
        android:id="@+id/login_button"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="38dp"
        android:layout_marginEnd="38dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/message"
        android:layout_below="@+id/login_button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="82dp" />


</RelativeLayout>

洛克特来了

代码语言:javascript
运行
复制
08-06 00:43:58.542    1904-1904/com.example.ebad.testing I/art﹕ Not late-enabling -Xcheck:jni (already on)
08-06 00:44:03.139    1904-1936/com.example.ebad.testing D/OpenGLRenderer﹕ Render dirty regions requested: true
08-06 00:44:03.142    1904-1904/com.example.ebad.testing D/﹕ HostConnection::get() New Host Connection established 0xa6c42500, tid 1904
08-06 00:44:03.203    1904-1904/com.example.ebad.testing D/Atlas﹕ Validating map...
08-06 00:44:03.473    1904-1917/com.example.ebad.testing I/art﹕ Background sticky concurrent mark sweep GC freed 3295(250KB) AllocSpace objects, 0(0B) LOS objects, 23% free, 872KB/1135KB, paused 39.997ms total 200.658ms
08-06 00:44:03.539    1904-1917/com.example.ebad.testing W/art﹕ Suspending all threads took: 66.163ms
08-06 00:44:03.821    1904-1911/com.example.ebad.testing W/art﹕ Suspending all threads took: 210.005ms
08-06 00:44:03.832    1904-1936/com.example.ebad.testing D/﹕ HostConnection::get() New Host Connection established 0xa6c42a30, tid 1936
08-06 00:44:03.838    1904-1917/com.example.ebad.testing I/art﹕ Background partial concurrent mark sweep GC freed 1888(102KB) AllocSpace objects, 0(0B) LOS objects, 55% free, 834KB/1858KB, paused 3.470ms total 295.680ms
08-06 00:44:03.892    1904-1936/com.example.ebad.testing I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-06 00:44:03.932    1904-1936/com.example.ebad.testing D/OpenGLRenderer﹕ Enabling debug mode 0
08-06 00:44:03.959    1904-1936/com.example.ebad.testing W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-06 00:44:03.959    1904-1936/com.example.ebad.testing W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae1de500, error=EGL_SUCCESS
08-06 00:44:03.970    1904-1917/com.example.ebad.testing W/art﹕ Suspending all threads took: 132.060ms
08-06 00:44:04.215    1904-1904/com.example.ebad.testing I/Choreographer﹕ Skipped 38 frames!  The application may be doing too much work on its main thread.
08-06 00:44:04.823    1904-1904/com.example.ebad.testing I/Choreographer﹕ Skipped 36 frames!  The application may be doing too much work on its main thread.
08-06 00:44:05.503    1904-1936/com.example.ebad.testing W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-06 00:44:05.503    1904-1936/com.example.ebad.testing W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae1de500, error=EGL_SUCCESS
08-06 00:44:24.394    1904-1911/com.example.ebad.testing W/art﹕ Suspending all threads took: 5.553ms
08-06 00:45:10.805    1904-1904/com.example.ebad.testing W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://111.68.99.8 refused
08-06 00:45:10.805    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
08-06 00:45:10.805    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-06 00:45:10.805    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-06 00:45:10.805    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-06 00:45:10.805    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at com.example.ebad.testing.MainActivity$1.onClick(MainActivity.java:82)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at android.view.View.performClick(View.java:4756)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at android.view.View$PerformClick.run(View.java:19749)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
08-06 00:45:10.806    1904-1904/com.example.ebad.testing W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ Caused by: java.net.ConnectException: socket failed: EACCES (Permission denied)
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:181)
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ ... 17 more
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:623)
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ at java.net.Socket.checkOpenAndCreate(Socket.java:687)
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ at java.net.Socket.connect(Socket.java:847)
08-06 00:45:10.807    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-06 00:45:10.808    1904-1904/com.example.ebad.testing W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-06 00:45:10.808    1904-1904/com.example.ebad.testing W/System.err﹕ ... 17 more
08-06 00:45:10.808    1904-1904/com.example.ebad.testing W/System.err﹕ Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
08-06 00:45:10.808    1904-1904/com.example.ebad.testing W/System.err﹕ at libcore.io.Posix.socket(Native Method)
08-06 00:45:10.808    1904-1904/com.example.ebad.testing W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
08-06 00:45:10.808    1904-1904/com.example.ebad.testing W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:608)
08-06 00:45:10.808    1904-1904/com.example.ebad.testing W/System.err﹕ ... 22 more
08-06 00:45:10.808    1904-1904/com.example.ebad.testing D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
08-06 00:45:10.809    1904-1904/com.example.ebad.testing E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.ebad.testing, PID: 1904
    java.lang.NullPointerException: Attempt to invoke interface method 'org.apache.http.StatusLine org.apache.http.HttpResponse.getStatusLine()' on a null object reference
            at com.example.ebad.testing.MainActivity$1.onClick(MainActivity.java:86)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-06 00:45:13.333    1904-1904/com.example.ebad.testing I/Process﹕ Sending signal. PID: 1904 SIG: 9

AndroidManifest.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ebad.testing" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-permission android:name="android.permission.INTERNET" />
    </application>

</manifest>

提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-04 21:39:39

当您单击登录按钮时,您将获得一个"NetworkOnMainThreadException“。这种异常是由于在UI线程上与网络一起工作造成的。

您可以使用异步任务或使用某些库来管理网络通信。我建议你使用Volley库。它很容易使用。

http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html https://developer.android.com/training/volley/index.html

票数 2
EN

Stack Overflow用户

发布于 2015-08-04 23:29:57

创建内部类并扩展AsyncTask以执行后台登录操作。应该在UI线程上执行像网络连接这样的长时间运行的操作。

票数 0
EN

Stack Overflow用户

发布于 2015-08-05 19:54:02

从日志里说:

代码语言:javascript
运行
复制
org.apache.http.conn.HttpHostConnectException: Connection to http://111.68.99.8 refused

这意味着服务器不接受您的连接。

重新键入您的权限(在AndroidManifest.xml中),因为您可能遇到了一些问题。此外,编辑你的问题,并张贴在那里,我们可以帮助,如果我们可以看到它完全。

编辑:

虽然不应该在主线程(或用户接口)上执行网络操作,但这似乎不是主要问题。

08-06 00:45:10.808 1904-1904/com.example.ebad.testing W/System.err﹕ Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31819601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档