我试图从其URL下载给定网站的HTML。 执行此操作的代码位于异步任务中,与UI分开,并且URL传递给它,这正常工作。
设置了HttpClient,并创建了一个请求,但是在执行请求时,抛出了IOexception。 我不明白为什么会发生这种情况,因为网址有效,并且该应用具有网络访问权限。
public class RetrieveData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String url = params[0];
Log.v("The URL is", url);
try {
HttpClient client = new DefaultHttpClient();
Log.v("Progress", "Got to HttpClient");
HttpGet request = new HttpGet(url);
Log.v("Progress", "Got to request");
HttpResponse response = client.execute(request);
Log.v("Progress", "Got to execute request");
String html = "";
Log.v("Progress", "Got to String html");
InputStream in = response.getEntity().getContent();
Log.v("Progress", "Got to InputStream");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
Log.v("The string is", str.toString());
}
in.close();
html = str.toString();
String TAG = "html extracted";
Log.v(TAG, html);
} catch (Exception IOexception) {
Log.v("IOexception ", "IOexception thrown in RetrieveData.java");
}
return null;
}
}
感谢所有帮助。
堆栈追踪 -
07-07 11:32:40.130 2168-2185/? W/System.err﹕ java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.google.com
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:596)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:298)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at com.anapp.tpb.downloadhtml.RetrieveData.doInBackground(RetrieveData.java:43)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at com.anapp.tpb.downloadhtml.RetrieveData.doInBackground(RetrieveData.java:29)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-07 11:32:40.130 2168-2185/? W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:2)
(来自评论)当您想要调用URL时,您必须从:
开始 http://
或https://
对于文件(用于比较)
file:///
或
ftp://
以指定使用的协议
答案 1 :(得分:1)
您错过了URL
请求中的架构,添加了http
或https
:
path=www.google.com
必须为path=https://www.google.com