即使我在另一个线程上运行,我似乎也在Android上获得MainThreadException。代码和跟踪如下。任何人都可以告诉我下面我做错了什么。除非我弄错了,postDelayed()处理程序不在主线程上,它从内部调用register(),然后调用DavidWebb代码。我错过了什么?
final boolean b = new Handler().postDelayed(
new Runnable() {
public void run() {
if (register(name, email, password)) {
onSignupSuccess();
} else {
onSignupFailed();
}
progressDialog.dismiss();
}
}, 3000);
private boolean register(String name, String email, String password) {
String json = "{ "
+ "\"tenant\" : {"
+ "\"name\" : \"" + name + "\""
+ "},"
+ "\"user\" : {"
+ "\"email\" : \"" + email + "\","
+ "\"password\" : \"" + encryptPassword(password) + "\""
+ "}"
+ "}";
Webb webb = Webb.create();
webb.setDefaultHeader("Content-Type", "application/json");
Response<JSONObject> response = webb.post(bigskyRegister).body(json).asJsonObject();
if (!response.isSuccess()) {
Log.d(TAG, "Auth fail: " + response.getStatusCode() +
" - " + response.getResponseMessage());
webb.delete(bigskyRegister).asVoid();
return false;
}
webb.delete(bigskyRegister).asVoid();
return false;
}
Stack trace:
com.goebl.david.WebbException: android.os.NetworkOnMainThreadException
at com.goebl.david.Webb.execute(Webb.java:329)
at com.goebl.david.Request.asJsonObject(Request.java:257)
at com.vmware.bsky.SignupActivity.register(SignupActivity.java:145)
at com.vmware.bsky.SignupActivity.access$000(SignupActivity.java:22)
at com.vmware.bsky.SignupActivity$3.run(SignupActivity.java:79)
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:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:217)
at com.goebl.david.Webb.writeBody(Webb.java:361)
at com.goebl.david.Webb.execute(Webb.java:295)
at com.goebl.david.Request.asJsonObject(Request.java:257)
at com.vmware.bsky.SignupActivity.register(SignupActivity.java:145)
at com.vmware.bsky.SignupActivity.access$000(SignupActivity.java:22)
at com.vmware.bsky.SignupActivity$3.run(SignupActivity.java:79)
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:5254)
at java.lang.reflect.Method.invoke(Native Method)
感谢您的帮助。