Android- 执行 HTML 请求导致 IOexception
Android- Executing HTML request results in IOexception
我正在尝试从给定网站的 URL 下载其 HTML。
执行此操作的代码在异步任务中,与 UI 分开,并且 URL 传递给它,工作正常。
设置了HttpClient,创建了一个请求,但是执行请求的时候抛出IOexception。
我不明白为什么会这样,因为 URL 有效,并且该应用程序可以访问网络。
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.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.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)
(来自评论)当你想调用一个URL时,你必须从:
开始
http://
或 https://
对于文件(用于比较)
file:///
或
ftp://
为了指定使用的协议
您在 URL
请求中缺少 架构 ,请添加 http
或 https
:
path=www.google.com
必须是 path=https://www.google.com
我正在尝试从给定网站的 URL 下载其 HTML。 执行此操作的代码在异步任务中,与 UI 分开,并且 URL 传递给它,工作正常。
设置了HttpClient,创建了一个请求,但是执行请求的时候抛出IOexception。 我不明白为什么会这样,因为 URL 有效,并且该应用程序可以访问网络。
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.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.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)
(来自评论)当你想调用一个URL时,你必须从:
开始http://
或 https://
对于文件(用于比较)
file:///
或
ftp://
为了指定使用的协议
您在 URL
请求中缺少 架构 ,请添加 http
或 https
:
path=www.google.com
必须是 path=https://www.google.com