0

我对 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);

但我仍然收到此错误。

不知道我在哪里犯了错误。

提前致谢。

4

2 回答 2

2

引起:android.os.NetworkOnMainThreadException

您正在尝试在 UI 线程上进行网络调用。

因此,使用 AsyncTask 来完成这些任务。

在 Android 3.0 + 版本中,任何可能阻塞 UI 的网络调用或操作都必须在后台线程上完成。

编辑:

试试这个onCreate()

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
于 2013-02-18T07:34:51.743 回答
1

我见过有人AsyncTask这样执行。new MyAsyncTask().doInBackground();而不是new MyAsyncTask().execute();. 请确保您不是无意中犯了这个错误。

于 2013-02-18T08:01:03.690 回答