-1

I'm trying to read a JSON from a server but not'm getting

  public JSONObject getJSON(){ 
  JSONObject jsonObject = null;
  try{
    // Create a new HTTP Client
    DefaultHttpClient defaultClient = new DefaultHttpClient();
    // Setup the get request
    HttpGet httpGetRequest = new HttpGet("URL");

    // Execute the request in the client
    HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
    // Grab the response
    BufferedReader reader = new BufferedReader(new                          
        InputStreamReader(httpResponse.getEntity().
        getContent(), "UTF-8"));
    String json = reader.readLine();

    // Instantiate a JSON object from the request response
    jsonObject = new JSONObject(json);

} catch(Exception e){
    // In your production code handle any errors and catch the individual exceptions
    e.printStackTrace();
}
return jsonObject;

}
Exception:

 android.os.NetworkOnMainThreadException

I'm using tags permission to use internet in XML Android

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

1 Answers1

0

Try to do that with AsyncTask since network mustnt be on main thread

yossico
  • 3,421
  • 5
  • 41
  • 76