2014-12-24 58 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)); 

不是简单的打印方法。