-3

I am inserting the data from android to mysql using php and json request. When the event fire it gives the exception in the logcat. I am sharing my clogcat with you so you can have better look and idea of what is going on , here is my logcat :

10-07 03:55:13.073: E/AndroidRuntime(1665): FATAL EXCEPTION: main 10-07 03:55:13.073: E/AndroidRuntime(1665): android.os.NetworkOnMainThreadException 10-07 03:55:13.073: E/AndroidRuntime(1665): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133) 10-07 03:55:13.073: E/AndroidRuntime(1665): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 10-07 03:55:13.073: E/AndroidRuntime(1665): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 10-07 03:55:13.073: E/AndroidRuntime(1665): at java.net.InetAddress.getAllByName(InetAddress.java:214) 10-07 03:55:13.073: E/AndroidRuntime(1665): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 10-07 03:55:13.073: E/AndroidRuntime(1665): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 10-07 03:55:13.073: E/AndroidRuntime(1665): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 10-07 03:55:13.073: E/AndroidRuntime(1665): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 10-07 03:55:13.073: E/AndroidRuntime(1665): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 10-07 03:55:13.073: E/AndroidRuntime(1665): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 10-07 03:55:13.073: E/AndroidRuntime(1665): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 10-07 03:55:13.073: E/AndroidRuntime(1665): at com.example.receiptorganizer.Registration$1.insert(Registration.java:78) 10-07 03:55:13.073: E/AndroidRuntime(1665): at com.example.receiptorganizer.Registration$1.onClick(Registration.java:58) 10-07 03:55:13.073: E/AndroidRuntime(1665): at android.view.View.performClick(View.java:4240) 10-07 03:55:13.073: E/AndroidRuntime(1665): at android.view.View$PerformClick.run(View.java:17721) 10-07 03:55:13.073: E/AndroidRuntime(1665): at android.os.Handler.handleCallback(Handler.java:730) 10-07 03:55:13.073: E/AndroidRuntime(1665): at android.os.Handler.dispatchMessage(Handler.java:92) 10-07 03:55:13.073: E/AndroidRuntime(1665): at android.os.Looper.loop(Looper.java:137) 10-07 03:55:13.073: E/AndroidRuntime(1665): at android.app.ActivityThread.main(ActivityThread.java:5103) 10-07 03:55:13.073: E/AndroidRuntime(1665): at java.lang.reflect.Method.invokeNative(Native Method) 10-07 03:55:13.073: E/AndroidRuntime(1665): at java.lang.reflect.Method.invoke(Method.java:525) 10-07 03:55:13.073: E/AndroidRuntime(1665): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 10-07 03:55:13.073: E/AndroidRuntime(1665): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-07 03:55:13.073: E/AndroidRuntime(1665): at dalvik.system.NativeStart.main(Native Method)

4

3 回答 3

1

This error comes when you are handling a network call(making a webservice call) in the main thread i.e. in the UI thread of the application.

Use AsyncTask or Handler to manage your webservice call.

see this answer

Your async task class can be like this

class TheTask extends AsyncTask<Void,Void,Void>
{
          protected void onPreExecute()
          {           super.onPreExecute();
                    //display progressdialog.
          } 

           protected void doInBackground(Void ...params)
          {  
                //http request. do not update ui here

                return null;
          } 

           protected void onPostExecute(Void result)
          {     
                    super.onPostExecute(result);
                    //dismiss progressdialog.
                    //update ui
          } 

}

about AsyncTask and Handler

And you can use handler like this :

Handler handler = new Handler();
handler.post(new Runnable() {

    @Override
    public void run() {
    // TODO Auto-generated method stub
     // network call here
        }
    });

Use of AsyncTask is a better solution.

于 2013-10-07T08:10:51.170 回答
0

Please look for this exception in google android.os.NetworkOnMainThreadException or just follow a tutroial

于 2013-10-07T08:12:31.843 回答
0

use AsyncTask or Thread to call webservice to send data from your application.

于 2013-10-07T08:17:19.633 回答