2011-12-24 119 views
0

我有一个为我的android应用程序提供数据的url,我从教程中学习并编写了一些代码。它完全适用于其他的URL,但这个无法检索JSON响应

http://acolyteserv.appspot.com/Products/getProductMatchedList/?format=json&p0=galaxy&p1=4&p2=all 

代码:

private void testere() 
{ 
    InputStream is = null; 
    String result; 
    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://acolyteserv.appspot.com/Products/getProductMatchedList/?format=json&p0=galaxy&p1=4&p2=all"); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 


     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

      StringBuilder sb = new StringBuilder(); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 

      } 

      is.close(); 
      result=sb.toString(); 


      Toast t=Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG); 
      t.show(); 
    }catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

    } 
    catch(Exception e){ 
     Log.e("log_tag", "Error in http connection "+e.toString()); 
    } 


} 

如果我用这个URL敬酒,结果给我一个空字符串,但如果我使用例如URL像

http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo 

请告诉我我在做什么错了,我是JSON和整个Web服务世界的新手。

回答

1

将您的请求添加到您的HttpEntity中。或者使用HttpGet代替

+0

感谢您指出了这一点!愚蠢的错误,它的工作很好。改变了它。我实际上使用了一个例子,它具有我删除的那些参数,因为我知道它不会对我有用。 – 2011-12-24 18:04:52

1

您正在使用HttpPost,但未提交任何实体请求。使用HttpGet代替。鉴于你的网址等东西的名称,我怀疑一个GET是你真正想要的。

+0

谢谢你,改变了它,它的工作正常。 – 2011-12-24 18:05:16