HttpURLConnection:在一些android设备和模拟器中获得307响应代码
我正在使用HttpURLConnection:在一些android设备和模拟器中获得307响应代码,android,httpurlconnection,http-status-codes,Android,Httpurlconnection,Http Status Codes,我正在使用HttpURLConnection执行对特定URL的GET请求。在一些模拟器和设备中,它工作得很好,我得到了200个代码,在另一些设备中我得到了307个代码。有人能告诉我问题出在哪里吗 这是我的密码: URL cnx = new URL(url); HttpURLConnection urlCon = (HttpURLConnection) cnx.openConnection(); urlCon.setRequestMethod("GET"); urlCon.setConnect
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解决了我的问题。如果有人有同样的问题,下面是代码: 公共字符串WebServiceCall(字符串url){
307是临时重定向。无法说明它发生的原因,但通常大多数http客户端都可以“自动”执行这些重定向。您是否尝试将代码放入线程中?实际上,我正在将代码放入线程中。
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;
}