我在android应用程序中关于以下代码有一个不寻常的问题。Android中的奇怪HttpResponse
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")
当在模拟器上运行,该代码工作正常,如果本地主机宕机,“源”如预期返回为空。同样,在移动设备上运行代码(HTC Nexus One,Android版本2.2)时,代码会将“源”报告为空。 但是,当在其他移动设备(Samsung Galaxy S2,Android版本4.0.3,samsung版本)上运行相同的APK时,返回的状态代码将作为HttpStatus.SC_OK给出,即使它不可能连接到本地主机。有没有人遇到类似的问题?
如何在Galaxy S2上安装/运行它,与模拟器/ HTC Nexus One相同(通过adb debug),或直接安装并运行apk而无需adb连接? – yorkw 2012-07-18 21:54:58
嗨,感谢您的评论,我已经尝试了两种方法,没有运气。 – user1015492 2012-07-18 22:23:39