Http Get Request返回html?

时间:2014-12-24 12:12:02

标签: android json http android-activity

我使用http get Request在android中加载数据。不知道它返回html而不是JSON结果。在浏览器中加载时,相同的URL会获得json,但响应时会显示html。

我的Http Get call格式就像这样...

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();

3 个答案:

答案 0 :(得分:1)

您是否掌控了目标网络服务?

您是否尝试过“text / x-json”作为内容类型。最近发现自己有些系统不支持application / json,即使它是标准的。

答案 1 :(得分:1)

服务器必须返回JSON,而不仅仅是打印。 例如,如果您使用PHP,请使用:

print(json_encode($response));

不是简单的打印方法。

答案 2 :(得分: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);
        }