我还是编程界的新手。我正在尝试为我们公司的网页创建一个应用程序。我尝试研究了大约一个月。我正在逐步构建它,但我的进度非常缓慢,因为我仍然无法解决这个问题。我的应用程序的问题是当我通过模拟器登录时,它会加载,但在加载对话框之后它没有响应。即使我的密码正确或错误,它也不会响应。如果密码正确或错误,它应该会出现。请帮助并纠正我在代码中的错误。
先感谢您!!!
这是我的代码:
package com.hahndroid.loginactivity;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
String AIM_LOGIN = "http://vo.aimglobalinc.com/index.asp"; // <- this is the URL of our company portal/webpage
String AIM_CONTENTS = "http://vo.aimglobalinc.com/r2bs.asp"; // <- this is the URL of our company portal/webpage appears on the browser when you login successfully or when you open your portal
String user_id;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button signin = (Button) findViewById(R.id.btn_login);
signin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText etxt_user = (EditText) findViewById(R.id.txt_username);
user_id = etxt_user.getText().toString();
EditText etxt_password = (EditText) findViewById(R.id.txt_password);
password = etxt_password.getText().toString();
new LoginTask().execute();
}
});
}
private boolean doLogin(String user_id, String password) {
String line = "";
StringBuilder total = new StringBuilder();
boolean result=false;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(AIM_LOGIN);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("userid", user_id));
nameValuePairs.add(new BasicNameValuePair("password",password));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
if(response.getStatusLine().getStatusCode() < 400)
{
//Intent intent = new Intent(LoginActivity.this, RunApp.class);
//startActivity(intent);
String cookie = response.getFirstHeader("Set-Cookie").getValue();
Toast.makeText(LoginActivity.this, "Welcome to AIM Global" , Toast.LENGTH_SHORT).show(); // this should appear when login is successful
HttpGet getContents = new HttpGet(AIM_CONTENTS);
getContents.setHeader("Cookie", cookie);
response = httpClient.execute(getContents);
InputStream ins = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
while ((line = br.readLine()) != null) {
total.append(line);
}
}else
{
Toast.makeText(LoginActivity.this, "Please check your Username and Password" , Toast.LENGTH_SHORT).show(); // this should appear when login is wrong
}
}
catch(SocketException ex)
{
Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
}
catch (Exception e) {
Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
e.printStackTrace();
}
return result;
}
private class LoginTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
LoginActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Logging in...");
this.dialog.show();
}
protected Void doInBackground(final Void... unused) {
boolean auth=doLogin(user_id,password);
System.out.println(auth);
return null;
}
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
}
我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hahndroid.loginactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.hahndroid.loginactivity.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以下是我尝试登录我们网页时的日志:
09-09 04:30:34.538: I/Choreographer(902): Skipped 33 frames! The application may be doing too much work on its main thread.
09-09 04:30:35.028: D/gralloc_goldfish(902): Emulator without GPU emulation detected.
09-09 04:31:12.168: D/InputEventConsistencyVerifier(902): KeyEvent: ACTION_UP but key was not down.
09-09 04:31:12.168: D/InputEventConsistencyVerifier(902): in android.widget.EditText{40d014c8 VFED..CL .F....I. 128,192-278,232 #7f080001 app:id/txt_password}
09-09 04:31:12.168: D/InputEventConsistencyVerifier(902): 0: sent at 605400000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=605400, downTime=605353, deviceId=0, source=0x101 }
09-09 04:31:24.638: D/dalvikvm(902): GC_CONCURRENT freed 141K, 10% free 2658K/2944K, paused 100ms+10ms, total 312ms
09-09 04:31:24.638: D/dalvikvm(902): WAIT_FOR_CONCURRENT_GC blocked 77ms
09-09 04:31:25.498: I/Choreographer(902): Skipped 53 frames! The application may be doing too much work on its main thread.
09-09 04:31:26.333: I/Choreographer(902): Skipped 32 frames! The application may be doing too much work on its main thread.
09-09 04:31:40.219: D/dalvikvm(902): GC_CONCURRENT freed 63K, 8% free 3006K/3244K, paused 88ms+127ms, total 804ms
09-09 04:31:40.351: I/Choreographer(902): Skipped 55 frames! The application may be doing too much work on its main thread.
09-09 04:31:41.289: I/Choreographer(902): Skipped 30 frames! The application may be doing too much work on its main thread.
09-09 04:31:42.369: I/Choreographer(902): Skipped 32 frames! The application may be doing too much work on its main thread.
09-09 04:31:42.639: E/Error :(902): Error on soapPrimitiveData() Can't create handler inside thread that has not called Looper.prepare()
09-09 04:31:43.114: W/System.err(902): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-09 04:31:43.299: W/System.err(902): at android.os.Handler.<init>(Handler.java:197)
09-09 04:31:43.344: W/System.err(902): at android.os.Handler.<init>(Handler.java:111)
09-09 04:31:43.431: W/System.err(902): at android.widget.Toast$TN.<init>(Toast.java:324)
09-09 04:31:43.431: W/System.err(902): at android.widget.Toast.<init>(Toast.java:91)
09-09 04:31:43.439: W/System.err(902): at android.widget.Toast.makeText(Toast.java:238)
09-09 04:31:43.439: W/System.err(902): at com.hahndroid.loginactivity.LoginActivity.doLogin(LoginActivity.java:81)
09-09 04:31:43.499: W/System.err(902): at com.hahndroid.loginactivity.LoginActivity.access$0(LoginActivity.java:56)
09-09 04:31:43.549: I/Choreographer(902): Skipped 33 frames! The application may be doing too much work on its main thread.