通过app登录我的大学生门户网站

时间:2015-08-04 21:24:01

标签: android json httpclient

我想通过我的应用程序连接到我大学的学生门户网站,并访问网站上的某些信息,如当前结果。所以我写了这段代码到目前为止我无法登录。每当我按下登录按钮时,应用程序崩溃..我知道我的代码出了问题,所以如果你们帮我修复问题我会非常感激..这里是我学生档案的网址。

http://111.68.99.8/StudentProfile/

这是我到目前为止编写的代码....

java文件

Val

这是xml文件......

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);
    }
}

Logcat就在这里

<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>

的AndroidManifest.xml

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

提前致谢。

3 个答案:

答案 0 :(得分:2)

单击“登录”按钮时,将获得“NetworkOnMainThreadException”。这种异常是由于在UI线程上使用网络。

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

来自Logcat:

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)

相关问题