gpt4 book ai didi

android - 如何在android中使用asyncTask

转载 作者:行者123 更新时间:2023-11-30 03:06:43 26 4
gpt4 key购买 nike

我是 android 的新手..我试图解决 networkonmainthreadexception,所以我使用了异步任务但仍然遇到相同的异常.. 出了什么问题??这是我的 onCreate 方法

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Connection().execute();
}

这是我的连接方式

private class Connection extends AsyncTask<Object, Object, Object>
{
@Override
protected void onPreExecute() {
preExecuteLogin();
}
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
doInBackgroundLogin();
return null;
}


}

这是预执行方法

 public void preExecuteLogin()
{
txtUserName=(EditText)this.findViewById(R.id.txtUsername);
txtPassword=(EditText)this.findViewById(R.id.txtPassword);
buttonLogin=(ImageButton)this.findViewById(R.id.buttonLogin);
}

这是我的 doInBackgroundLogin 方法

 public void doInBackgroundLogin()
{

buttonLogin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
showToast("Login successfull");

String link = "http://www.quinoid.com/project/AndroidXml/Login.php?username="
+ txtUserName.getText().toString() + "&password=" + txtPassword.getText().toString();
try {

JSONObject json = jParser.getJSONFromUrl(link);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent myIntent = new Intent(MainActivity.this, secondpage.class);
startActivity(myIntent);
} else{
showToast("Invalid Login");
}

}
});
}

那么我究竟做错了什么?我的堆栈跟踪

02-11 03:17:38.797: E/AndroidRuntime(1324): FATAL EXCEPTION: main
02-11 03:17:38.797: E/AndroidRuntime(1324): Process: com.example.qwedd, PID: 1324
02-11 03:17:38.797: E/AndroidRuntime(1324): android.os.NetworkOnMainThreadException
02-11 03:17:38.797: E/AndroidRuntime(1324): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
02-11 03:17:38.797: E/AndroidRuntime(1324): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-11 03:17:38.797: E/AndroidRuntime(1324): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-11 03:17:38.797: E/AndroidRuntime(1324): at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-11 03:17:38.797: E/AndroidRuntime(1324): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
02-11 03:17:38.797: E/AndroidRuntime(1324): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-11 03:17:38.797: E/AndroidRuntime(1324): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-11 03:17:38.797: E/AndroidRuntime(1324): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-11 03:17:38.797: E/AndroidRuntime(1324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-11 03:17:38.797: E/AndroidRuntime(1324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-11 03:17:38.797: E/AndroidRuntime(1324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-11 03:17:38.797: E/AndroidRuntime(1324): at com.example.connection.JSONParser.getJSONFromUrl(JSONParser.java:38)
02-11 03:17:38.797: E/AndroidRuntime(1324): at com.example.qwedd.MainActivity$2.onClick(MainActivity.java:68)
02-11 03:17:38.797: E/AndroidRuntime(1324): at android.view.View.performClick(View.java:4438)
02-11 03:17:38.797: E/AndroidRuntime(1324): at android.view.View$PerformClick.run(View.java:18422)
02-11 03:17:38.797: E/AndroidRuntime(1324): at android.os.Handler.handleCallback(Handler.java:733)
02-11 03:17:38.797: E/AndroidRuntime(1324): at android.os.Handler.dispatchMessage(Handler.java:95)
02-11 03:17:38.797: E/AndroidRuntime(1324): at android.os.Looper.loop(Looper.java:136)
02-11 03:17:38.797: E/AndroidRuntime(1324): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-11 03:17:38.797: E/AndroidRuntime(1324): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 03:17:38.797: E/AndroidRuntime(1324): at java.lang.reflect.Method.invoke(Method.java:515)
02-11 03:17:38.797: E/AndroidRuntime(1324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-11 03:17:38.797: E/AndroidRuntime(1324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-11 03:17:38.797: E/AndroidRuntime(1324): at dalvik.system.NativeStart.main(Native Method)
02-11 03:22:38.957: I/Process(1324): Sending signal. PID: 1324 SIG: 9

最佳答案

其实你是

打电话

Toast.makeTextlogin() 方法中。

Toast 只能显示在 UI 线程上。

要么从 login() 方法中删除 Toast

或者像这样调用 Toast

private void showToast(String msg){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, msg,Toast.LENGTH_LONG).show();
}
});
}

完整的解决方案

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtUserName=(EditText)this.findViewById(R.id.txtUsername);
txtPassword=(EditText)this.findViewById(R.id.txtPassword);
buttonLogin=(ImageButton)this.findViewById(R.id.buttonLogin);

buttonLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Connection().execute();
}
});
}

public void doInBackgroundLogin() {
if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
showToast("Login successfull");

String link = "http://www.quinoid.com/project/AndroidXml/Login.php?username="
+ txtUserName.getText().toString() + "&password=" + txtPassword.getText().toString();
try {
JSONObject json = jParser.getJSONFromUrl(link);
} catch (JSONException e) {
e.printStackTrace();
}

Intent myIntent = new Intent(MainActivity.this, secondpage.class);
startActivity(myIntent);
} else{
showToast("Invalid Login");
}
}

关于android - 如何在android中使用asyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695876/

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