我对Android很新,我得到一个像这样的例外:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplepay.hellomobile/com.simplepay.hellomobile.CardPayment}: android.os.NetworkOnMainThreadException
stacktrace在这里:
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;
}
其中initOrder
是一个普通的java方法,它有一行代码:
responseBody = httpClient.execute(post,responseHandler);
但我仍然收到此错误。
不知道我在哪里弄错了。
提前致谢。
答案 0 :(得分:2)
引起:android.os.NetworkOnMainThreadException
您正尝试在UI线程上进行网络调用。
因此,使用AsyncTask来完成此类操作。
在Android 3.0 +版本中,强制要求任何可能阻止UI的网络调用或操作必须在后台线程上完成。
修改强>
在onCreate()
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
答案 1 :(得分:1)
我见过像这样执行AsyncTask
的人。 new MyAsyncTask().doInBackground();
代替new MyAsyncTask().execute();
。请确保您没有无意中犯这个错误。