Android 获取api 23及以下版本中的Http状态400

Android 获取api 23及以下版本中的Http状态400,android,httpconnection,Android,Httpconnection,我必须调用GET请求的一个RESTAPI 当我使用API 24的模拟器时,它工作得很好。但我手机上的相同代码,即API 23和API 18,它给出400状态,即错误请求 URL url = new URL(voids[0]); Log.i("url", voids[0]); //URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openCon

我必须调用GET请求的一个RESTAPI

当我使用API 24的模拟器时,它工作得很好。但我手机上的相同代码,即API 23和API 18,它给出400状态,即错误请求

 URL url = new URL(voids[0]);
        Log.i("url", voids[0]);
        //URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.addRequestProperty("User-Agent", "REST");
        System.setProperty("http.keepAlive", "false");
        con.setRequestProperty("Accept", "*//*");
        con.setConnectTimeout(10000);
        con.setReadTimeout(10000);
        con.setAllowUserInteraction(false);
        con.setRequestMethod("GET");
       responseCode = con.getResponseCode();
        System.out.println("Sending get request : " + url);
        System.out.println("Response code : " + responseCode);
        // Reading response from input Stream
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String output;
        StringBuffer response = new StringBuffer();
        while ((output = in.readLine()) != null) {
            response.append(output);
        }
        in.close();
        System.out.println(response.toString());

Url:

首先尝试对Url的
模型
参数进行编码。 您可以使用
URLEncoder.encode()
方法来实现这一点