关于 android 应用程序中的以下代码,我遇到了一个不寻常的问题。
public InputStream retrieveStream(String url) {
HttpGet request = new HttpGet(url);
HttpParams httpParams = new BasicHttpParams();
int some_reasonable_timeout = (int) (10 * DateUtils.SECOND_IN_MILLIS);
HttpConnectionParams.setConnectionTimeout(httpParams, some_reasonable_timeout);
HttpConnectionParams.setSoTimeout(httpParams, some_reasonable_timeout);
HttpClient client = new DefaultHttpClient(httpParams);
try
{
HttpResponse response = client.execute(request);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK){
HttpEntity getResponseEntity = response.getEntity();
return getResponseEntity.getContent();
}
else
{
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
}
catch (ClientProtocolException e)
{
Log.e("Methods", "HTTP Error", e);
return null;
}
catch (IOException e)
{
Log.e("Methods", "Connection Error", e);
return null;
}
}
在应用程序中,我尝试连接到本地主机。(在 AsyncTask 中)
InputStream source = retrieveStream("http://10.0.2.2:27080/testdb/_hello")
在模拟器上运行时,此代码工作正常,如果 localhost 关闭,“source”按预期返回为 null。同样,在移动设备(HTC Nexus One,Android 版本 2.2)上运行代码时,代码按预期将“源”报告为 null。但是,当在另一台移动设备(Samsung Galaxy S2,Android 版本 4.0.3,三星版本)上运行相同的 APK 时,返回的状态代码为 HttpStatus.SC_OK,即使它不可能连接到 localhost。有没有人遇到过类似的问题?