使用HttpUrlConnection获取非常通用的IOException

时间:2012-11-23 14:53:10

标签: java android android-asynctask web-scraping httpurlconnection

我在使用HttpUrlConnection方面遇到了一些问题,当我运行该设备时(Gingerbread HTC Desire),LogCat显示的非常一般:

11-23 14:26:19.394: WARN/System.err(6591): java.io.IOException: Error connecting

我已经添加了AsyncTask来尝试解决问题,但似乎没有区别。我已经在底部显示了完整的logcat,这里是有问题的类(缩短了一点并将URL设置为google(结果相同):

public class JSoupHelper {
    private static final String LOGIN_URL = "http://www.google.co.uk";

    public JSoupHelper() {
        new Login().execute(LOGIN_URL);
    }
    private class Login extends AsyncTask<String, String, InputStream>{
        @Override
        protected InputStream doInBackground(String... params) {
            String url = params[0];
            Log.d("JSoup-LoginHelper","Starting background AsyncTask on "+url);
            try {
                InputStream is = OpenHttpConnection(url);
                return is;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
     private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        URL url = new URL(urlString);
        Log.d("JSoup-OHTTPSC","Connecting to "+urlString);
        try{
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setInstanceFollowRedirects(true);
            conn.setDoInput(true);
            conn.connect();
            Log.d("JSoup-OHTTPSC","Connected to "+urlString);

            int response = conn.getResponseCode();
            if (response == HttpsURLConnection.HTTP_OK) {
                in = conn.getInputStream();
            }
            conn.disconnect();
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");
        }
        return in;
    }
}

logcat的:

11-23 14:25:59.214: DEBUG/JSoup-LoginHelper(6591): Starting background AsyncTask on http://www.google.co.uk
11-23 14:25:59.234: DEBUG/JSoup-OHTTPSC(6591): Connecting to http://www.google.co.uk
           ~~~~ 20 secs pass ~~~~
11-23 14:26:19.394: WARN/System.err(6591): java.io.IOException: Error connecting
11-23 14:26:19.394: WARN/System.err(6591): at com.example.untitled1.JSoupHelper.OpenHttpsConnection(JSoupHelper.java:67)
11-23 14:26:19.394: WARN/System.err(6591): at com.example.untitled1.JSoupHelper.access$100(JSoupHelper.java:12)
11-23 14:26:19.394: WARN/System.err(6591): at com.example.untitled1.JSoupHelper$Login.doInBackground(JSoupHelper.java:40)
11-23 14:26:19.394: WARN/System.err(6591): at com.example.untitled1.JSoupHelper$Login.doInBackground(JSoupHelper.java:34)
11-23 14:26:19.394: WARN/System.err(6591): at android.os.AsyncTask$2.call(AsyncTask.java)
11-23 14:26:19.394: WARN/System.err(6591): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
11-23 14:26:19.394: WARN/System.err(6591): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-23 14:26:19.394: WARN/System.err(6591): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-23 14:26:19.394: WARN/System.err(6591): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-23 14:26:19.394: WARN/System.err(6591): at java.lang.Thread.run(Thread.java:1027)
11-23 14:26:23.077: INFO/htcfs-main(6749): main()

我的目标是实际获取一个InputStream并使用JSoup通过HttpsUrlConnection解析它,使用post登录然后保存cookie和会话变量,然后使用cookie我可以对网站进行适当的抓取并将结果存储到一个Sqlite数据库......但是一步一步(特别是由于我缺乏调试技巧和一般Java技能)。

提前致谢。

0 个答案:

没有答案