android - Http Get 请求返回 html?

标签 android json http android-activity

我正在使用 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/

相关文章:

jquery - AJAX Jsonp 调用在 IE9 中失败

jquery - 将 json 数据从 Controller 传递到 View

c# - 在 C# 中将对象转换为 Json 并通过 POST 发送它会导致对象损坏?

通过特定端口创建原始 HTTP 请求

http - 如何定义使用 'strict-dynamic' 但包含使用 'unsafe-inline' 的回退的内容安全策略 (CSP)?

android - 带有 % 百分号的 html 短信正文会使 android 应用程序崩溃

android - 想要与谷歌地图应用程序相同的卫星 View

java - 如何等待计数器完成

java - 文件未找到异常 : open failed: EACCES (Permission denied)

http - 设计合适的 REST URI