-6

我收到此错误。

10-03 03:20:54.995: E/AndroidRuntime(1422): FATAL EXCEPTION: main
10-03 03:20:54.995: E/AndroidRuntime(1422): android.os.NetworkOnMainThreadException
10-03 03:20:54.995: E/AndroidRuntime(1422):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at com.infotrans.library.JSONParser.getJSONFromUrl(JSONParser.java:142)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at com.infotrans.library.UserFunctions.registerUser(UserFunctions.java:58)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at com.infotrans.snapjug.activity.RegisterActivity.saveLoginDetails(RegisterActivity.java:132)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at com.infotrans.snapjug.activity.RegisterActivity.onClick(RegisterActivity.java:85)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at android.view.View.performClick(View.java:4240)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at android.view.View$PerformClick.run(View.java:17721)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at android.os.Handler.handleCallback(Handler.java:730)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at android.os.Looper.loop(Looper.java:137)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at java.lang.reflect.Method.invoke(Method.java:525)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-03 03:20:54.995: E/AndroidRuntime(1422):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

以它的方式使用AsyncTask 和执行您的网络操作doInBackground()

class Sample extends AsyncTask<Void, Void, Void> {

    private Exception exception;

    protected void doInBackground(String... urls) {
        try {
           // Your Network Operations
        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(Void result) {
        // TODO: check this.exception 
        // TODO: do something with the feed
    }
}
于 2013-10-03T07:24:44.940 回答