我必须调用一个GET请求的Rest API。
当我使用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());
答案 0 :(得分:1)
首先尝试对您网址的Model
参数进行编码。
您可以使用URLEncoder.encode()
方法