-2

In my application, I am working with Http services Like below:

 private void sendContacts() {
        try {
            contwithseparator = URLEncoder.encode(contwithseparator, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {    
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet("http://v.filmy.in/events/register/contacts.php?email=vinu@gmail.in&contacts="+contwithseparator);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);
            JSONArray jsonarray = new JSONArray(responseString);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String name = jsonobject.getString("name");
                String phn = jsonobject.getString("mobile");                        
            }  
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

But here when I try to hit the URL, it is hitting doesn't give any response Here is my log:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.system.costfinder, PID: 22355
                  android.os.NetworkOnMainThreadException
                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
                      at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
                      at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
                      at java.net.InetAddress.getAllByName(InetAddress.java:787)
                      at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
                      at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
                      at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
                      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
                      at com.example.system.costfinder.MainActivity.sendContacts(MainActivity.java:165)
                      at com.example.system.costfinder.MainActivity.access$000(MainActivity.java:49)
                      at com.example.system.costfinder.MainActivity$1.onClick(MainActivity.java:80)
                      at android.view.View.performClick(View.java:6256)
                      at android.view.View$PerformClick.run(View.java:24701)
                      at android.os.Handler.handleCallback(Handler.java:789)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Disconnected from the target VM, address: 'localhost:8681', transport: 'socket'

I can't understand what happening with this even I can't understand the log also please help me to solve this.

chanduuu
  • 19
  • 6

2 Answers2

0

This exception is thrown when an application attempts to perform a networking operation on its main thread. To avoid this, we can handle it using threads, executers or AsyncTask

Executors.newSingleThreadExecutor().submit(new Runnable() {
    @Override
    public void run() {
         HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet("http://v.filmy.in/events/register/contacts.php?email=vinu@gmail.in&contacts="+contwithseparator);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);
            JSONArray jsonarray = new JSONArray(responseString);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String name = jsonobject.getString("name");
                String phn = jsonobject.getString("mobile");                        
            }  
    }
});
Farhan Qasim
  • 990
  • 5
  • 18
  • Thanks for this replay, can you please help me to solve this – chanduuu Apr 12 '18 at 11:49
  • @chanduuu for a better answer visit this link : https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception It may help you correctly. Network services were restricted running on the main thread after Android Honeycomb, you need to run your function in Async mode to make it run. – Farhan Qasim Apr 12 '18 at 11:51
0

This is not an answer, but a suggestion. Check out http://square.github.io/retrofit/.

It will prevent from messy code that you have, easy to use. Testable.

Inkognito
  • 224
  • 1
  • 2
  • 10