我已经更新了脚本并减少了之前帖子中不必要的脚本。
HTTP客户端未在doinBackground函数的login.java文件中声明。
结果应显示为"登录成功"如果http响应是" 1"和#34;登录失败"如果http响应是" 0"。
但是我没有得到我期待的结果,我总是登录失败。当我还在字符串中编写Http Response值的显示时,它给了我" org.apache.http.message.BasicHttpResponse@40e79618"
############################################
login.java File
############################################
public class login extends Activity{
EditText un,pw;
TextView error;
Button ok;
public void onCreate(Bundle saveInstanceSate) {
super.onCreate(saveInstanceSate);
setContentView(R.layout.main);
un = (EditText)findViewById(R.id.et_un);
pw = (EditText)findViewById(R.id.et_pw);
error = (TextView)findViewById(R.id.tv_error);
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Action on click of the button btn_login
new MyHttpRequest().execute();
}
});
}
private class MyHttpRequest extends AsyncTask<String, Void, String>{
ProgressDialog progress;
public String doInBackground(String... urls){
ArrayList<NameValuePair> postinfo = new ArrayList<NameValuePair>();
postinfo.add(new BasicNameValuePair("username", un.getText().toString()));
postinfo.add(new BasicNameValuePair("password", pw.getText().toString()));
HttpClient MyhttpClient = new DefaultHttpClient();
HttpPost MyhttpPost = new HttpPost("http://10.0.0.41/test/check.php");
String response="";
// Start of Http post information
try{
MyhttpPost.setEntity(new UrlEncodedFormEntity(postinfo));
}catch (UnsupportedEncodingException e){
//writing error logs
e.printStackTrace();
}
// Start of HTTP Response Capture
try{
HttpResponse Myhttpresponse = MyhttpClient.execute(MyhttpPost);
Log.d("Http Response:", Myhttpresponse.toString());
// Convert my response into a string value
response = Myhttpresponse.toString();
// Decision based on response
if (response.equals("1")){
error.setText("Login Successfull. Result:" + " " +response);
}
else{
error.setText("Login failed. Result:" + " " + response);
}
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
// End of Do in Background
return response;
}
protected void onPreExecute(){
// Creates a progress message while connecting to the database
progress = new ProgressDialog(login.this);
progress.setMessage("Please wait while connecting...");
progress.show();
}
public void onPostExecute(String response){
super.onPostExecute(response);
if(progress.isShowing()){
progress.dismiss();
}
}
}
}
#
Debug Trace
#
02-16 23:36:55.345 4720-4897 / com.voicetone.login2 D / Http Response :: org.apache.http.message.BasicHttpResponse@40e80120 02-16 23:36:55.365 4720-4897 / com.voicetone.login2 W / dalvikvm:threadid = 12:线程退出未捕获异常(组= 0x40ab8228) 02-16 23:36:55.415 4720-4897 / com.voicetone.login2 E / AndroidRuntime:FATAL EXCEPTION:AsyncTask#1
java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:278) at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) at java.util.concurrent.FutureTask.setException(FutureTask.java:124) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:864) Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the
创建视图层次结构的原始线程可以触及其视图。 在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4132) 在android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:736) 在android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:785) 在android.view.ViewGroup.invalidateChild(ViewGroup.java:4013) 在android.view.View.invalidate(View.java:8614) 在android.view.View.invalidate(View.java:8565) 在android.widget.TextView.checkForRelayout(TextView.java:7117) 在android.widget.TextView.setText(TextView.java:3460) 在android.widget.TextView.setText(TextView.java:3316) 在android.widget.TextView.setText(TextView.java:3291) 在com.voicetone.login2.login $ MyHttpRequest.doInBackground(login.java:96) 在com.voicetone.login2.login $ MyHttpRequest.doInBackground(login.java:58) 在android.os.AsyncTask $ 2.call(AsyncTask.java:264) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) 在java.util.concurrent.FutureTask.run(FutureTask.java:137) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:208) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 在java.lang.Thread.run(Thread.java:864) com.android.internal.policy.impl.PhoneWindow$DecorView@40e5a668那个 最初是在这里添加的 android.view.WindowLeaked:活动com.voicetone.login2.login已泄露窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@40e5a668那个 最初是在这里添加的 在android.view.ViewRootImpl。(ViewRootImpl.java:352) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:373) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:321) 在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:152) 在android.view.Window $ LocalWindowManager.addView(Window.java:541) 在android.app.Dialog.show(Dialog.java:301) 在com.voicetone.login2.login $ MyHttpRequest.onPreExecute(login.java:114) 在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561) 在android.os.AsyncTask.execute(AsyncTask.java:511) 在com.voicetone.login2.login $ 1.onClick(login.java:51) 在android.view.View.performClick(View.java:3549) 在android.view.View $ PerformClick.run(View.java:14393) 在android.os.Handler.handleCallback(Handler.java:605) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:4945) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method)02-16 23:36:59.719 4720-4897 / com.voicetone.login2 D / Process:killProcess, pid = 4720 02-16 23:36:59.739 4720-4897 / com.voicetone.login2 D / Process:dalvik.system.VMStack.getThreadStackTrace(本机方法) 02-16 23:36:59.819 4720-4720 / com.voicetone.login2 W / IInputConnectionWrapper:非活动InputConnection上的showStatusIcon 02-16 23:36:59.919 4720-4720 / com.voicetone.login2 W / IInputConnectionWrapper:InputConnection = com.android.internal.widget.EditableInputConnection@40e50640,有效 client = false 02-16 23:37:00.049 4720-4897 / com.voicetone.login2 D / Process:java.lang.Thread.getStackTrace(Thread.java:599)
02-16 23:37:00.049 4720-4897 / com.voicetone.login2 D / Process:com.android.internal.os.RuntimeInit $ UncaughtHandler.uncaughtException(RuntimeInit.java:104) 02-16 23:37:00.059 4720-4897 / com.voicetone.login2 D / Process:java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) 02-16 23:37:00.059 4720-4897 / com.voicetone.login2 D / Process:java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 02-16 23:37:00.059 4720-4897 / com.voicetone.login2 I / Process:发送信号。 PID:4720 SIG:9 02-16 23:37:00.049 4720-4897 / com.voicetone.login2 D / Process:android.os.Process.killProcess(Process.java:790)
#我这次添加了你要求的痕迹。
请帮助我,如果有任何链接,你可以提供一些好的例子告诉我我的错误,我会跟着他们。
谢谢 Mad Rolly