【问题标题】:Android- Executing HTML request results in IOexceptionAndroid - 执行 HTML 请求导致 IOexception
【发布时间】:2015-09-24 20:49:46
【问题描述】:

我正在尝试从其 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$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 和/或你可以发布错误(IOexception 值)
  • 发布您的异常日志。在您的 catch 块中调用 IOexception.printStatckTrace() 以获取异常日志
  • 会的。我尝试了各种常见的站点,它们抛出了异常,然后是一些更晦涩的站点,其中一些输出了 HTML。
  • 默认情况下,尝试使用Log 的第二个参数来打印异常,而不是硬编码的字符串... Log 将说明从哪里引发异常... StackTraces很好:-)
  • http:// 添加到您的 url 调用中

标签: java android http ioexception


【解决方案1】:

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

http://https://

对于文件(用于比较)

file:///

ftp://

为了指定使用的协议

【讨论】:

  • 是的,这很容易。感谢您的帮助。
  • 正如我所说,,,StackTraces :D...而且更容易解决,如果你(我在这种情况下)以前见过它:-)(它从来没有帮助过)
  • 当您在这里时,有没有办法检查网站使用的是 http:// 还是 https://?或者当抛出异常时我是否必须尝试每个?
  • 我不能肯定地说,但我会说你必须尝试。因为http使用端口80ftp21https443。看看en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers 所用端口的不错选择(默认情况下,everything 都是可调整的)
【解决方案2】:

您在URL 请求中缺少架构,请添加httphttps

path=www.google.com 必须是 path=https://www.google.com

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 2018-08-24
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    相关资源
    最近更新 更多