gpt4 book ai didi

android - 通过应用程序登录我的大学生门户网站

转载 作者:行者123 更新时间:2023-11-29 20:31:57 24 4
gpt4 key购买 nike

我想用我的应用程序连接到我所在大学的学生门户网站,并从该网站访问某些信息,例如当前成绩。所以到目前为止我已经写了这段代码我在登录时遇到了问题。每当我按下登录按钮时应用程序崩溃..我知道我的代码有问题所以如果你们帮我解决问题我将非常感激..在这里是我的学生资料的 url。

http://111.68.99.8/StudentProfile/

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

java文件

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 文件...

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

Logcat 在这里

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

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

提前致谢。

最佳答案

当您单击“登录”按钮时,您会收到“NetworkOnMainThreadException”。这种异常是由于在 UI 线程上使用网络造成的。

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

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

关于android - 通过应用程序登录我的大学生门户网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31819601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com