java - Android-执行 HTML 请求会导致 IOException

标签 java android http ioexception

我正在尝试从给定网站的 URL 下载该网站的 HTML。 执行此操作的代码位于异步任务中,与 UI 分开,并且 URL 传递给它,工作正常。

HttpClient 已设置,并创建了请求,但在执行请求时,抛出了 IO 异常。 我不明白为什么会发生这种情况,因为 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$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)

最佳答案

(来自评论)当你想调用一个URL时,你必须以:

http://https://

对于文件(用于比较)

文件:///

ftp://

为了指定使用的协议(protocol)

关于java - Android-执行 HTML 请求会导致 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31267329/

相关文章:

java - JScrollPane 仅显示竖线和水平线的三个点

java - RXjava 实现对象更新的可观察对象

java - 如何将 SparseArray 转换为 ArrayList?

java - 如何在Java中执行HTTP Post请求?

events - 如何对事件循环进行单元测试?

java - 从 Java 执行 .bat 文件,为什么 Eclipse 项目目录很重要?

Java 映射字节缓冲区 - 缓冲区中的垃圾值

performance - 带有 .htaccess 的选择性 SSL 强制 http ://or https://

android - RSRuntimeException : Loading of ScriptC script failed (build tools 19. 0.2)

Android ListView 的麻烦