HttpURLConnection:在某些 android 设备和模拟器中获取 307 响应代码
HttpURLConnection: getting 307 response code in some android devices and emulators
我正在使用 HttpURLConnection
对特定 URL 执行 GET 请求。在某些模拟器和设备中,它工作得很好,我得到了 200 个代码,在其他模拟器和设备中,我得到了 307 个代码。有人可以告诉我问题是什么吗?
这是我的代码:
URL cnx = new URL(url);
HttpURLConnection urlCon = (HttpURLConnection) cnx.openConnection(); urlCon.setRequestMethod("GET");
urlCon.setConnectTimeout((int) timeout);
urlCon.setReadTimeout((int) timeout);
urlCon.connect();
int code = urlCon.getResponseCode();
if (code != 200) {
return null;
}
我已经通过使用 DefaultHttpClient 而不是 HttpURLConnection 解决了我的问题。如果有人遇到同样的问题,这里是代码:
public String WebServiceCall(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
res = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return res;
}
我正在使用 HttpURLConnection
对特定 URL 执行 GET 请求。在某些模拟器和设备中,它工作得很好,我得到了 200 个代码,在其他模拟器和设备中,我得到了 307 个代码。有人可以告诉我问题是什么吗?
这是我的代码:
URL cnx = new URL(url);
HttpURLConnection urlCon = (HttpURLConnection) cnx.openConnection(); urlCon.setRequestMethod("GET");
urlCon.setConnectTimeout((int) timeout);
urlCon.setReadTimeout((int) timeout);
urlCon.connect();
int code = urlCon.getResponseCode();
if (code != 200) {
return null;
}
我已经通过使用 DefaultHttpClient 而不是 HttpURLConnection 解决了我的问题。如果有人遇到同样的问题,这里是代码:
public String WebServiceCall(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
res = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return res;
}