2011-12-24 122 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