jsoup未知主机异常

时间:2013-04-13 19:11:02

标签: android jsoup

这是我已经工作了3天的代码。我无法找出它抛出的原因:

java.net.UnknownHostException:
Unable to resolve host "www.google.co.in": No address associated with hostname

以下是MainActivity类:

package com.example.yo;

import java.io.IOException;
import java.net.*;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class MainActivity extends Activity {

    Document doc;
    TextView tv;
    String res = "";

    String BLOG_URL = "https://www.google.co.in/";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // set layout view
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        new jsoupexample().execute(BLOG_URL);
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public class jsoupexample extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            try {
                doc = Jsoup.connect(BLOG_URL).userAgent("Mozilla/5.0").timeout(20000).get();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Elements links = doc.select("a[href]");

            for (Element element : links) {
                res = element.attr("href");
            }
            return res;
        }
    }

    protected void onPostExecute(String result) {
        if (result != null && !result.isEmpty()) {
            tv.setText(result);
        } else {
            tv.setText("no value returned");
        }
    }
}

这是logcat:

04-14 00:23:32.800: W/System.err(26397): java.net.UnknownHostException: Unable to resolve host "www.google.co.in": No address associated with hostname
04-14 00:23:32.810: W/System.err(26397):    at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
04-14 00:23:32.810: W/System.err(26397):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-14 00:23:32.820: W/System.err(26397):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:442)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
04-14 00:23:32.820: W/System.err(26397):    at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165)
04-14 00:23:32.830: W/System.err(26397):    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
04-14 00:23:32.830: W/System.err(26397):    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
04-14 00:23:32.830: W/System.err(26397):    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
04-14 00:23:32.830: W/System.err(26397):    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
04-14 00:23:32.830: W/System.err(26397):    at com.example.yo.MainActivity$jsoupexample.doInBackground(MainActivity.java:50)
04-14 00:23:32.830: W/System.err(26397):    at com.example.yo.MainActivity$jsoupexample.doInBackground(MainActivity.java:1)
04-14 00:23:32.830: W/System.err(26397):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-14 00:23:32.830: W/System.err(26397):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-14 00:23:32.830: W/System.err(26397):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-14 00:23:32.830: W/System.err(26397):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-14 00:23:32.830: W/System.err(26397):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-14 00:23:32.830: W/System.err(26397):    at java.lang.Thread.run(Thread.java:856)
04-14 00:23:48.490: W/dalvikvm(26397): threadid=11: thread exiting with uncaught exception (group=0x41767930)
04-14 00:23:48.510: E/AndroidRuntime(26397): FATAL EXCEPTION: AsyncTask #1
04-14 00:23:48.510: E/AndroidRuntime(26397): java.lang.RuntimeException: An error occured while executing doInBackground()
04-14 00:23:48.510: E/AndroidRuntime(26397):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at java.lang.Thread.run(Thread.java:856)
04-14 00:23:48.510: E/AndroidRuntime(26397): Caused by: java.lang.NullPointerException
04-14 00:23:48.510: E/AndroidRuntime(26397):    at com.example.yo.MainActivity$jsoupexample.doInBackground(MainActivity.java:57)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at com.example.yo.MainActivity$jsoupexample.doInBackground(MainActivity.java:1)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-14 00:23:48.510: E/AndroidRuntime(26397):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-14 00:23:48.510: E/AndroidRuntime(26397):    ... 4 more

2 个答案:

答案 0 :(得分:3)

发生异常java.net.UnknownHostException: Unable to resolve host是因为您的应用无法连接到指定的主机。

可能的原因是您的互联网连接实际上已关闭。您可以使用ping命令对其进行测试。

另一个常见原因是您无法在Android清单文件AndroidManifest.xml中访问互联网。

如果您尚未添加该权限,请将以下行添加到AndroidManifest.xml文件中,位于<manifest>标记之后和<application>标记之前:

<uses-permission android:name="android.permission.INTERNET" />

有关Android互联网权限语法和其他AndroidManifest权限示例的详细信息,请查看android.com的示例:http://developer.android.com/tools/samples/index.html

答案 1 :(得分:0)

你真的需要https吗?我建议尝试使用http