2011-03-08 106 views
0
HttpClient client = new DefaultHttpClient(); 
    String url = "http://maps.google.co.in/maps?q=restaurants&radius=5000&sll=23,72&output=json";  
    HttpGet request = new HttpGet(url); 
    HttpResponse response = client.execute(request); 
    if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 

    InputStream inputStream = response.getEntity().getContent(); 
    } 
    else 
    { 
     Toast.makeText(getBaseContext(), "error....", Toast.LENGTH_LONG); 
    } 

这是我简单的代码,NPE在JSON响应

但它崩溃了我的申请。向我展示NPE和未捕获的异常。 任何人都可以帮助我排除故障并解决此问题。

+0

它你JSON作为response.Doesn't这种权利似乎从你的代码。 – 2011-03-08 06:35:59

回答

0

请尝试运行下面的代码。

 HttpGet getMethod = new HttpGet("http://maps.google.co.in/maps?q=restaurants&radius=5000&sll=23,72&output=json"); 

     DefaultHttpClient hc = new DefaultHttpClient(); 

     HttpResponse response = hc.execute(getMethod); 
     HttpEntity entity = response.getEntity(); 

     // If the response does not enclose an entity, there is no need 
     // to worry about connection release 

     if (entity != null) 
     { 
      InputStream inStream = entity.getContent(); 
      result= convertStreamToString(inStream); 
      Log.i("------------------ Reulst",result); 
     } 

public static String convertStreamToString(InputStream is) 
{ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try 
    { 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     try 
     { 
      is.close(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 

}

+0

谢谢,但同样的错误发生。不用找了。 – AndroGeek 2011-03-08 06:28:35

+0

你可以发布错误日志.. – 2011-03-08 06:43:37

+0

上面的代码,我发布它工作正常我多数民众代表我要求错误日志 – 2011-03-08 07:01:18