【问题标题】:Http Get Request returns html?Http获取请求返回html?
【发布时间】:2014-12-24 12:12:02
【问题描述】:

我正在使用 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();

【问题讨论】:

  • 服务器如何决定发回哪种格式?您需要找出这一点,然后相应地编写 Android 应用程序。
  • 看起来您遇到了 404 html 页面或其他错误
  • 不,先生,我得到的响应代码为 200,响应代码只获取 html
  • 在海报插件(网络浏览器)中检查这个网络服务,然后在代码中测试。
  • 请检查返回结果的url。请将结果返回为 JSON Result 而不是 ActionResult

标签: android json http android-activity


【解决方案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);
        }

【讨论】:

    【解决方案2】:

    您是否在控制目标 Web 服务?

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

    【讨论】:

      【解决方案3】:

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

      print(json_encode($response));
      

      不是简单的打印方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-24
        • 2017-01-10
        • 1970-01-01
        • 2021-03-09
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        相关资源
        最近更新 更多