これは、以下のコードで直面している状況です。
ご覧のとおり、HTTP ストリームを読み取ろうとしています。Android シミュレーターで次のコードを実行すると、100% の時間で動作します。3G で次のコードを Galaxy S3 で実行すると、ラップトップを使用して URL に接続しようとすると、100% の時間で動作します。 Galaxy S3ブラウザー(wifiと3gの両方)を使用して接続しようとすると、100%動作します... 100%の時間。ただし、Wi-Fi で Galaxy S3 を使用して接続しようとすると、約 80% の時間でタイムアウトします。タイムアウト プロパティを削除すると、次のような奇妙な例外が発生します。
"recvfrom failed: ETIMEDOUT"
"failed to connect to <URL>: connect failed: ENETUNREACH (Network is unreachable)"
"unable to resolve the host <URL>: no address associated with hostname"
私はどんな提案にもオープンです...
public static final String getHttpResponse(String url)
{
HttpURLConnection conn = null;
InputStream response = null;
try {
URL address = new URL(url);
conn = (HttpURLConnection)address.openConnection();
conn.setConnectTimeout(30 * 1000); //30 seconds
conn.setReadTimeout(30 * 1000); //30 seconds
response = conn.getInputStream();
if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.e("Util.getHttpResponse", Integer.toString(conn.getResponseCode()));
return null;
}
String result = Util.streamToString(response);
return result;
} catch(IOException e) {
response = conn.getErrorStream();
Log.e("Util.getHttpResponse", Util.streamToString(response));
return null;
} finally {
if( response != null ) {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
conn.disconnect();
}
}
}
更新: - AndroidHttpClient の使用が機能しませんでした - 入力ストリームを取得した後、Eclipse IDE でエラー ポップアップが表示されました...ご覧のとおり、デバッグ カーソルが 107 行目まで表示されました。今回の入力ストリーム...