我正在使用 http get 请求在 android 中加载数据。不知道它返回 html 而不是 JSON 结果。相同的 URL 在浏览器中加载时获取 json,但响应来自 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();
最佳答案
一旦你也检查了服务器端代码.. 在即将到来之后意味着像这样更改代码...
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);
}
关于android - Http Get 请求返回 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27636777/