-1

This is the URL i am calling --

private String url="http://mywebsite.com/requiredfolder/myfile.php";

This is the json output getting from the above URL --

{"wallpapers":[{"id":"9","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_X5x6MVgv_VID-20171202-WA0012.mp4","downloads":"0","fav":"0"},
{"id":"8","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_eFmWtS@I_Funny Kid playing.mp4","downloads":"0","fav":"0"},
{"id":"7","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_trE3U&%x_VID_20171019_163735.mp4","downloads":"0","fav":"0"},
{"id":"6","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_ROgIfyHG_Assamesefunny.mp4","downloads":"0","fav":"0"},
{"id":"3","video_url":"http://mywebsite.com/AndroidApp/VideoUploads/raman.das@gmail.com_C2z$XU6!_Bombay Ka Brahman.mp4","downloads":"0","fav":"0"}],"success":1,"count":"5"}

Now in my Java file when trying to read the same using the given code --

try
        {
            DefaultHttpClient defaultClient = new DefaultHttpClient();
            Log.d("Message : ","DefaultHttpClient done");
            HttpGet httpGetRequest = new HttpGet(url);
            Log.d("Message : ","HttpGet done");
            HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
            Log.d("Message : ","HttpResponse done");
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            Log.d("Message : ","BufferedReader done");

            result = reader.readLine();
            //response = CustomHttpClient.executeHttpGet(url);
            //Log.d("Message : ","response done");
            //result = response.toString();
            Log.d("Message : ","result done");
            json = new JSONObject(result);
         }
          catch (Exception e) {
            Log.e("log_tag","Error in http connection!!" + e.toString());
         }

In the above code, in line --

HttpResponse httpResponse = defaultClient.execute(httpGetRequest);

I am getting the runtime execption.

E/log_tag: Error in http connection!!android.os.NetworkOnMainThreadException

Struggled a lot but not getting any idea, any lights on my code will be great.

Nayan Das
  • 75
  • 7

2 Answers2

0

NetworkOnMainThreadException

The exception that is thrown when an application attempts to perform a networking operation on its main thread. Try implementing your logic in a different background thread.

NetworkOnMainThreadException

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
islamdidarmd
  • 725
  • 5
  • 19
0

https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html NetworkOnMainThreadException read this

abuosama
  • 19
  • 2
  • 3