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;
}
}
アプリケーション内で、localhost に接続しようとします (AsyncTask 内)。
InputStream source = retrieveStream("http://10.0.2.2:27080/testdb/_hello")
エミュレーターで実行すると、このコードは正常に機能し、localhost がダウンしている場合は、期待どおり "source" が null として返されます。同様に、モバイル デバイス (HTC Nexus One、Android バージョン 2.2) でコードを実行すると、コードは予想どおり "source" を null として報告します。ただし、別のモバイル デバイス (Samsung Galaxy S2、Android バージョン 4.0.3、samsung リリース) で同じ APK を実行すると、返されるステータス コードは HttpStatus.SC_OK となります。誰かが同様の問題に遭遇しましたか?