2017-07-11 50 views
-4

我使用發佈人來測試網址(後),它看起來不錯。 API:http://www.dmapp.com.tw/MobileApp/GetHealthData.php爲什麼我無法獲取json數據?

但是當我嘗試在我的機器人,我不能得到的消息,甚至我的響應代碼爲200

我logcat的顯示{"Msg":"Request method not accepted","JsonData":null,"ErrorCode":"0099"}

我不明白這一點,我用這個代碼很好地獲得了json數據。

有人可以教我什麼我想念它,這將不勝感激。

這裏是我的GET JSON數據功能:

private String getRoute(String url) throws IOException { 
     HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
     int responseCode = connection.getResponseCode(); 
     StringBuilder jsonIn = new StringBuilder(); 
     // responseCode show 200 
     Log.d("responseCode:",responseCode+""); 
     if (responseCode == 200) { 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       jsonIn.append(line); 
      } 
     } else { 
      Log.d(TAG, responseCode + "responseCode"); 
     } 
     connection.disconnect(); 
     // I can't get the json data it shows Request method not accepted 
     Log.d(TAG, jsonIn + "jsonIn"); 
     return jsonIn.toString(); 

    } 
+0

您鏈接的鏈接有這個呃ror消息。 – andras

+0

謝謝我現在得到答案。 –

回答

2

HttpURLConnection默認發送GET請求。
您的鏈接完美運行。
嘗試增加
connection.setRequestMethod("POST");

Reference

+0

謝謝山姆!你救我 ! –

1

從服務器的響應本身是空的JsonData,你應該檢查在服務器端。

1

你好你的鏈接工作它只是它的一個請求

private String getRoute(String url) throws IOException { 
     HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
     int responseCode = connection.getResponseCode(); 
     connection.setRequestMethod("POST"); 


     StringBuilder jsonIn = new StringBuilder(); 
     // responseCode show 200 
     Log.d("responseCode:",responseCode+""); 
     if (responseCode == 200) { 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       jsonIn.append(line); 
      } 
     } else { 
      Log.d(TAG, responseCode + "responseCode"); 
     } 
     connection.disconnect(); 
     // I can't get the json data it shows Request method not accepted 
     Log.d(TAG, jsonIn + "jsonIn"); 
     return jsonIn.toString(); 

    } 

我希望它會工作,但在我看來,你應該一起工作排氣或改造,而不是HttpURLConnection

相關問題