Enrique Benitez
Enrique Benitez

Reputation: 679

Android UnknownHostException when using HttpGet

i have developing an android application that requests an httpget and then it converts to string and put it on a textview, but as far i got is a annoying error on the emulator and on my android device, here it is:

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

And here is my code:

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();
            }
        }
    }

What im doing wrong? Thanks and have a nice day!

Upvotes: 0

Views: 914

Answers (2)

kosa
kosa

Reputation: 66637

UnknowHosts error happens in the case where your app couldn't reach the host configured in URL.

Steps to make sure are:

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.

One of these steps should help you in resolving the issue.

Upvotes: 1

Arslan Anwar
Arslan Anwar

Reputation: 18746

Don't forget to add internet permission in your AndroidManifest.xml file.

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

Upvotes: 2

Related Questions