使用HttpGet时的Android UnknownHostException

时间:2012-01-26 05:44:10

标签: android

我开发了一个请求httpget的Android应用程序,然后它转换为字符串并将其放在textview上,但到目前为止,我在模拟器和我的Android设备上遇到了一个恼人的错误,这里是:

01-26 05:08:25.346: D/AndroidRuntime(523): Shutting down VM
01-26 05:08:25.346: W/dalvikvm(523): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-26 05:08:25.366: E/AndroidRuntime(523): FATAL EXCEPTION: main
01-26 05:08:25.366: E/AndroidRuntime(523): java.lang.IllegalStateException: Could not execute method of the activity
01-26 05:08:25.366: E/AndroidRuntime(523):  at android.view.View$1.onClick(View.java:2144)
01-26 05:08:25.366: E/AndroidRuntime(523):  at android.view.View.performClick(View.java:2485)
01-26 05:08:25.366: E/AndroidRuntime(523):  at android.view.View$PerformClick.run(View.java:9080)
01-26 05:08:25.366: E/AndroidRuntime(523):  at android.os.Handler.handleCallback(Handler.java:587)
01-26 05:08:25.366: E/AndroidRuntime(523):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 05:08:25.366: E/AndroidRuntime(523):  at android.os.Looper.loop(Looper.java:123)
01-26 05:08:25.366: E/AndroidRuntime(523):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-26 05:08:25.366: E/AndroidRuntime(523):  at java.lang.reflect.Method.invokeNative(Native Method)
01-26 05:08:25.366: E/AndroidRuntime(523):  at java.lang.reflect.Method.invoke(Method.java:507)
01-26 05:08:25.366: E/AndroidRuntime(523):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-26 05:08:25.366: E/AndroidRuntime(523):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-26 05:08:25.366: E/AndroidRuntime(523):  at dalvik.system.NativeStart.main(Native Method)
01-26 05:08:25.366: E/AndroidRuntime(523): Caused by: java.lang.reflect.InvocationTargetException
01-26 05:08:25.366: E/AndroidRuntime(523):  at java.lang.reflect.Method.invokeNative(Native Method)
01-26 05:08:25.366: E/AndroidRuntime(523):  at java.lang.reflect.Method.invoke(Method.java:507)
01-26 05:08:25.366: E/AndroidRuntime(523):  at android.view.View$1.onClick(View.java:2139)
01-26 05:08:25.366: E/AndroidRuntime(523):  ... 11 more
01-26 05:08:25.366: E/AndroidRuntime(523): Caused by: java.net.UnknownHostException: www.conecciones.net
01-26 05:08:25.366: E/AndroidRuntime(523):  at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
01-26 05:08:25.366: E/AndroidRuntime(523):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
01-26 05:08:25.366: E/AndroidRuntime(523):  at java.net.InetAddress.getAllByName(InetAddress.java:256)
01-26 05:08:25.366: E/AndroidRuntime(523):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
01-26 05:08:25.366: E/AndroidRuntime(523):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-26 05:08:25.366: E/AndroidRuntime(523):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-26 05:08:25.366: E/AndroidRuntime(523):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
01-26 05:08:25.366: E/AndroidRuntime(523):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-26 05:08:25.366: E/AndroidRuntime(523):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-26 05:08:25.366: E/AndroidRuntime(523):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-26 05:08:25.366: E/AndroidRuntime(523):  at sys.temperature.ConvertActivity.executeHttpGet(ConvertActivity.java:82)
01-26 05:08:25.366: E/AndroidRuntime(523):  at sys.temperature.ConvertActivity.myClickHandler(ConvertActivity.java:70)
01-26 05:08:25.366: E/AndroidRuntime(523):  ... 14 more
01-26 05:08:29.187: I/Process(523): Sending signal. PID: 523 SIG: 9

这是我的代码:

public void executeHttpGet(String cis) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        Toast.makeText(this, "http://www.**************.net/******/test.php?ci=" + cis, 2).show();            
        HttpGet request = new HttpGet(http://www.**************.net/******/test.php?ci=" + cis);
        //request.setURI(new URI("http://w3mentor.com/"));
        HttpResponse response = client.execute(request);
        in = new BufferedReader
        (new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        String page = sb.toString();
        System.out.println(page);
        } finally {
        if (in != null) {
            try {
                in.close();
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

我做错了什么? 谢谢,祝你有愉快的一天!

2 个答案:

答案 0 :(得分:2)

不要忘记在AndroidManifest.xml文件中添加互联网权限。

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

答案 1 :(得分:1)

如果您的应用无法访问在网址中配置的主机,则会发生UnknowHosts错误。

确保的步骤是:

1) INTERNET permission enabled in manifest file
2) Are you able to access the same url from phone browser
3) The ports (if any) are open on the server
4) If you are using host name, try with IP address.

其中一个步骤可以帮助您解决问题。