Java Android - HttpURLConnection - Android 4.x.x上的响应代码400

时间:2017-11-16 15:21:58

标签: java android httpurlconnection

我使用以下代码:

        System.setProperty("http.keepAlive","false");
        String content;
        mainURL += "&fingerprint=" + Build.FINGERPRINT + "&devicename=" + "" + "&devicemodel=" + Build.MANUFACTURER + " - " + Build.MODEL;
        try {
            URL url = new URL(mainURL);
            HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
            urlconn.setConnectTimeout(HTTP_REQUEST_TIMEOUT);
            urlconn.setReadTimeout(HTTP_REQUEST_TIMEOUT);
            urlconn.setRequestMethod("GET");
            urlconn.connect();

            int responseCode = urlconn.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK){
                content = readStream(urlconn.getInputStream());
            }else{
                content = "error";
            }

            urlconn.disconnect();
        } catch (Exception e) {
            return "error";
        }

        return content;

这在Android 6.x.x和Android 7.x.x上运行良好,但在Android 4.4.2上,它返回了http响应代码400.这个问题可能是由于这个原因导致的:

mainURL += "&fingerprint=" + Build.FINGERPRINT + "&devicename=" + "" + "&devicemodel=" + Build.MANUFACTURER + " - " + Build.MODEL;

提前致谢。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。

Build无法在Android 4.x.x上运行,这也是HttpURLConnection无法正常工作的原因。