我对 Android 很陌生,但遇到这样的异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplepay.hellomobile/com.simplepay.hellomobile.CardPayment}: android.os.NetworkOnMainThreadException
堆栈跟踪在这里:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
at org.apache.http.impl.io.SocketInputBuffer.isStale(SocketInputBuffer.java:109)
at org.apache.http.impl.AbstractHttpClientConnection.isStale(AbstractHttpClientConnection.java:205)
at org.apache.http.impl.conn.AbstractClientConnAdapter.isStale(AbstractClientConnAdapter.java:185)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:336)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
at juspay.services.WalletService.makeServiceCall(WalletService.java:283)
at juspay.services.WalletService.getCardTokens(WalletService.java:237)
at com.simplepay.hellomobile.CardPayment.addContentTo(CardPayment.java:107)
at com.simplepay.hellomobile.CardPayment.setUpLayoutAndActionListeners(CardPayment.java:81)
at com.simplepay.hellomobile.CardPayment.onCreate(CardPayment.java:56)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
请注意,所有网络调用都是使用该方法完成的,AsyncTask
并放置在该doInBackground
方法中。代码是:
protected JSONObject doInBackground(Void... voids)
{
JSONObject initOrderResponse = walletService.initOrder(this.amount);
return initOrderResponse;
}
whereinitOrder
是一个普通的 java 方法,它有如下一行:
responseBody = httpClient.execute(post,responseHandler);
但我仍然收到此错误。
不知道我在哪里犯了错误。
提前致谢。