-4

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)

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
geekdev786
  • 33
  • 1
  • 6
  • I think you should have put some more effort into this before asking someone else to solve your problem. Simply pasting `android.os.NetworkOnMainThreadException ` into Google would have provided the answer for you. To understand the down votes please refer to the [help](http://stackoverflow.com/help) page. :) – Qben Oct 07 '13 at 08:20

3 Answers3

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.

Community
  • 1
  • 1
Shruti
  • 1
  • 13
  • 55
  • 95
  • thank you , so what will be the best solution for that ? – geekdev786 Oct 07 '13 at 08:12
  • I have updated the answer,please see it – Shruti Oct 07 '13 at 08:28
  • @shurti thank you right now i am not getting any error exception but the simulator said that application stopped working so its due to slow internet or anyother issue ? – geekdev786 Oct 07 '13 at 10:15
  • @shurti can you please tell me why i am facing this ? 10-07 06:27:30.293: W/System.err(2829): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.10.101 refused 10-07 06:27:30.293: W/System.err(2829): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183) 10-07 06:27:30.293: W/System.err(2829): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) – geekdev786 Oct 07 '13 at 10:38
  • if you are using emulator to run your app for local server. mention the local ip as 10.0.2.2 and have to give Internet permission into your app – Shruti Oct 07 '13 at 10:42
0

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

A.S.
  • 4,574
  • 3
  • 26
  • 43
0

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

user8938
  • 559
  • 1
  • 4
  • 12