2014-12-24 57 views
4

我正在使用http get請求在android中加載數據。不知道它返回的是html而不是JSON結果。當在瀏覽器中加載時,相同的URL會獲得json,但是會響應html。Http Get Request返回html?

我的HTTP GET調用格式就像這個...

url = new URL(urlString); 

     //httpURLConnection = (HttpsURLConnection) url.openConnection(); 
     httpURLConnection = (HttpURLConnection) url.openConnection(); 

     httpURLConnection.setRequestMethod("GET"); 
     httpURLConnection.setRequestProperty("Connection", "keep-alive"); 

     httpURLConnection.setRequestProperty("Accept", "application/json"); // or application/jsonrequest 
     httpURLConnection.setRequestProperty("Content-Type", "application/json"); 
     /* 
     httpURLConnection.setRequestProperty("Content-Type", 
       "application/json");*/ 
     httpURLConnection.setRequestProperty("UseCookieContainer", "True"); 
     httpURLConnection.setRequestProperty("Cookie", cokieValue); 
     httpURLConnection.connect(); 
+0

服務器如何決定發送哪種格式回來?你需要找到它,然後相應地編寫Android應用程序。 – Henry

+3

看起來像你正在得到404 html頁面或其他一些錯誤 – injecteer

+0

沒有先生我得到的響應代碼爲200,響應代碼只得到html – Sankar

回答

1

一旦你檢查服務器端代碼以及.. 後即將手段改變這樣的代碼...

url = new URL(urlString); 

     httpURLConnection = (HttpsURLConnection) url.openConnection(); 
     //httpURLConnection = (HttpURLConnection) url.openConnection(); 
    // HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier()); 
     httpURLConnection.setRequestMethod("GET"); 
     httpURLConnection.setRequestProperty("Connection", "keep-alive"); 
     httpURLConnection.setRequestProperty("Content-Type", 
       "application/json"); 
     httpURLConnection.setRequestProperty("UseCookieContainer", "True"); 
     httpURLConnection.setChunkedStreamingMode(0); 
     httpURLConnection.connect(); 

     if (httpURLConnection != null) { 
      respCode = httpURLConnection.getResponseCode(); 
      messageStatus.setResponseCode(respCode); 
     } 
     if (respCode == 200) { 
      InputStream responseStream = httpURLConnection.getInputStream(); 
      messageStatus.setResponseStream(responseStream); 
     } 
1

你是在對目標Web服務的控制權?

您是否嘗試過「text/x-json」作爲內容類型。最近發現我自己有些系統不支持application/json,即使它是標準的。

1

服務器必須返回JSON,而不僅僅是打印。 如果您使用PHP爲例,請使用:

print(json_encode($response)); 

不是簡單的打印方法。