Android 通过应用程序登录我的大学生门户
我想用我的应用程序连接到我所在大学的学生门户网站,并访问网站上的某些信息,如当前结果。所以我写了这段代码,到目前为止,我登录时遇到了问题。每当我按下登录按钮,应用程序就会崩溃。。我知道我的代码有问题,所以如果你们能帮我解决这个问题,我将非常感激。。这是我的学生档案的url 这是我到目前为止写的代码 java文件Android 通过应用程序登录我的大学生门户,android,json,httpclient,Android,Json,Httpclient,我想用我的应用程序连接到我所在大学的学生门户网站,并访问网站上的某些信息,如当前结果。所以我写了这段代码,到目前为止,我登录时遇到了问题。每当我按下登录按钮,应用程序就会崩溃。。我知道我的代码有问题,所以如果你们能帮我解决这个问题,我将非常感激。。这是我的学生档案的url 这是我到目前为止写的代码 java文件 package com.example.ebad.testing; import android.support.v7.app.ActionBarActivity; import a
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);
}
}
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线程上使用网络造成的 您可以使用异步任务或使用某些库来管理网络通信。我建议你使用截击库。它很容易使用
创建内部类并扩展AsyncTask以在后台执行登录操作。 长时间运行的操作(如网络连接)应在UI线程之外执行。从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﹕ 原因:android.system.ErrnoException:socket失败:EACCES(权限被拒绝)
您可以共享崩溃的logcat输出吗?这会很有帮助你遇到了“麻烦”--麻烦的性质是什么?@StephenG先生,我已经添加了logcat,如果你能帮我的话,我将非常感谢:)@DaveNewton当我输入注册和密码并按下登录按钮登录时,应用程序崩溃。我添加了androidMainfest。xml@Ebadali检查编辑,搜索错误。这很可能是权限问题。我建议删除
,然后在应用程序元素中的第一个元素内部手动重新写入它。先生,我不知道它到底出了什么问题。。。我根据自己的需要编辑了这段代码,但我对httpclient和其他东西不太了解。我手动编辑了manifest.xml,但应用程序仍在崩溃。我们可以在聊天室聊天或讨论问题吗?谢谢。@DaveNewton我只是浏览Stackoverflow,并在工作时间尽量帮助他人,所以我不会使用聊天。从我在这里的本地尝试中,我注意到我有很长的延迟,并且结果是正确的。你似乎还有其他问题。。。。您是通过设备还是AVD进行测试?此外,访问手机浏览器中的链接,查看主机是否允许移动访问数据。
<?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>
org.apache.http.conn.HttpHostConnectException: Connection to http://111.68.99.8 refused