我有一个有趣的问题,我试图使用 HttpURLConnection 下载 XML 文件。该代码在 android 4.2 上运行良好,但不适用于 2.3.4
任何帮助将不胜感激
代码如下。
package com.test.testdownloader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button) findViewById(R.id.button1))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
downloadtest();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
void downloadtest() throws IOException {
URL url = new URL("http://someurl/list.xml");
new DownloadTask().execute(url);
}
private class DownloadTask extends AsyncTask<URL, Integer, String> {
@Override
protected String doInBackground(URL... urls) {
try {
URL url = urls[0];
InputStream is = inputStreamForUrl(url);
String result = getStringFromInputStream(is);
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
// convert InputStream to String
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
public static InputStream inputStreamForUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.setConnectTimeout(30000);
urlConnection.setReadTimeout(30000);
urlConnection.connect();
return urlConnection.getInputStream();
}
}
它在 android 4.2 和 4.3 中运行良好,但在 2.3.4 中会引发此错误
08-21 11:15:55.634: W/System.err(5719): java.net.SocketTimeoutException: Connection timed out
08-21 11:15:55.644: W/System.err(5719): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
08-21 11:15:55.644: W/System.err(5719): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
08-21 11:15:55.654: W/System.err(5719): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
08-21 11:15:55.654: W/System.err(5719): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
08-21 11:15:55.654: W/System.err(5719): at java.net.Socket.connect(Socket.java:983)
08-21 11:15:55.664: W/System.err(5719): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
08-21 11:15:55.664: W/System.err(5719): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
08-21 11:15:55.664: W/System.err(5719): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
08-21 11:15:55.674: W/System.err(5719): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
08-21 11:15:55.674: W/System.err(5719): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
08-21 11:15:55.674: W/System.err(5719): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
08-21 11:15:55.684: W/System.err(5719): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
08-21 11:15:55.684: W/System.err(5719): at com.test.testdownloader.MainActivity.inputStreamForUrl(MainActivity.java:99)
08-21 11:15:55.694: W/System.err(5719): at com.test.testdownloader.MainActivity$DownloadTask.doInBackground(MainActivity.java:51)
08-21 11:15:55.694: W/System.err(5719): at com.test.testdownloader.MainActivity$DownloadTask.doInBackground(MainActivity.java:1)
08-21 11:15:55.694: W/System.err(5719): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-21 11:15:55.694: W/System.err(5719): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-21 11:15:55.694: W/System.err(5719): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-21 11:15:55.694: W/System.err(5719): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-21 11:15:55.694: W/System.err(5719): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-21 11:15:55.694: W/System.err(5719): at java.lang.Thread.run(Thread.java:1019)