2014-01-28 3 views
-1

Я пытаюсь читать JSON с сервера, но not'm получениеЧтение JSON с Android

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; 

}
Исключение:

android.os.NetworkOnMainThreadException 

Я использую теги разрешения на использование интернет в XML Android

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

http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Raghunandan

ответ

0

Попробуйте сделать это с помощью AsyncTask, так как сеть не должно быть на главной нити

Смежные вопросы