0

I followed this tutorial for AsyncTask and getting the following error log:

03-23 11:44:42.936: WARN/System.err(315): java.net.UnknownHostException: www.google.co.in  
03-23 11:44:42.936: WARN/System.err(315):     at java.net.InetAddress.lookupHostByName(InetAddress.java:513)  
03-23 11:44:42.936: WARN/System.err(315):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)  
03-23 11:44:42.936: WARN/System.err(315):     at java.net.InetAddress.getAllByName(InetAddress.java:242)  
03-23 11:44:42.936: WARN/System.err(315):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)  
03-23 11:44:42.936: WARN/System.err(315):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)    
03-23 11:44:42.936: WARN/System.err(315):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-23 11:44:42.936: WARN/System.err(315):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)  
03-23 11:44:42.936: WARN/System.err(315):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-23 11:44:42.936: WARN/System.err(315):       at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-23 11:44:42.944: WARN/System.err(315):     at    org.apache.http.impl.client.AbstractHttpC  lient.execute(AbstractHttpClient.java:465)
03-23 11:44:42.944: WARN/System.err(315):     at com.test.async.AsyncTaskExampleActivity$DownloadWebPageTask.doInBackground   (AsyncTaskExampleActivity.java:36)
03-23 11:44:42.944: WARN/System.err(315):     at com.test.async.AsyncTaskExampleActivity$DownloadWebPageTask.doInBackground    (AsyncTaskExampleActivity.java:1)  
03-23 11:44:42.944: WARN/System.err(315):     at android.os.AsyncTask$2.call(AsyncTask.java:185)  
03-23 11:44:42.944: WARN/System.err(315):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-23 11:44:42.944: WARN/System.err(315):     at java.util.concurrent.FutureTask.run   (FutureTask.java:137)  
03-23 11:44:42.944: WARN/System.err(315):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-23 11:44:42.944: WARN/System.err(315):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-23 11:44:42.944: WARN/System.err(315):     at java.lang.Thread.run(Thread.java:1096)  

How do i fix it??

My Code:

 public class AsyncTaskExampleActivity extends Activity {  
    private TextView textView;

    /** Called when the activity is first created. */
        @Override
            public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
             textView = (TextView) findViewById(R.id.TextView01);
     }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
       @Override
        protected String doInBackground(String... urls) {
          String response = "";
           Log.i("", "in doInBackgroundddddddddd..........");
           Log.i("", "in readWebpageeeeeeeeeeeee");
           /*
           * try { InetAddress i =
           * InetAddress.getByName("http://google.co.in"); } catch
           * (UnknownHostException e1) { e1.printStackTrace(); }
           */
          for (String url : urls) {
            Log.i("", "in for looooooop doInBackgroundddddddddd..........");
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                Log
                        .i("",
                                "afetr for looooooop try doInBackgroundddddddddd..........");
                HttpResponse execute = client.execute(httpGet); 
                Log
                        .i("",
                                "afetr for looooooop try client ..execute doInBackgroundddddddddd..........");
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                    Log
                            .i("",
                                    "afetr while looooooop try client ..execute doInBackgroundddddddddd..........");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Log
                .i("",
                        "afetr lasttttttttttttt b4 response  doInBackgroundddddddddd..........");
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.i("", "in onPostExecuteeee..........");
        textView.setText(result);
    }
}

public void readWebpage(View view) {
    /*
     * System.setProperty("http.proxyHost", "10.132.116.10");
     * System.setProperty("http.proxyPort", "3128");
     */

    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://google.co.in" });
    Log.i("", "in readWebpageeeeeeeeeeeee after execute..........");

}

}

main.xml:

<Button android:id="@+id/readWebpage" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:onClick="readWebpage"
    android:text="Load Webpage">
</Button>

<TextView android:id="@+id/TextView01" android:layout_width="match_parent"
    android:layout_height="match_parent" android:text="Example Text">
</TextView>

Manifest:

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.test.async" android:versionCode="1" android:versionName="1.0">
   <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".AsyncTaskExampleActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

Thanks
Sneha

Smitha
  • 6,110
  • 24
  • 90
  • 161

3 Answers3

0

Just add the following two lines before your call to web:

try { InetAddress i = InetAddress.getByName("YourDesiredURL"); }
catch (UnknownHostException e1) { e1.printStackTrace(); }

You don't need to catch any exception with this, just execute it before your web calls. For more info, read this

Community
  • 1
  • 1
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • did u mean this? public void readWebpage(View view) { /*System.setProperty("http.proxyHost", "10.132.116.10"); System.setProperty("http.proxyPort", "3128"); */ Log.i("","in readWebpageeeeeeeeeeeee"); try { InetAddress i = InetAddress.getByName("http://google.co.in"); } catch (UnknownHostException e1) { e1.printStackTrace(); } DownloadWebPageTask task = new DownloadWebPageTask(); task.execute(new String[] { "http://google.co.in"}); Log.i("","in readWebpageeeeeeeeeeeee after execute.........."); } – Smitha Mar 23 '12 at 07:54
  • just add those above two lines in doInBackground() method of your async task – waqaslam Mar 23 '12 at 07:58
0

Tried your code and got the same error to solve the error, I replace two lines of code that is

HttpGet httpGet = new HttpGet(url); 
HttpResponse execute = client.execute(httpGet); 

By,

HttpPost httppost = new HttpPost(url); 
HttpResponse execute = client.execute(httppost);  

EDIT:

Also added this line after creating HttpClient object.

HttpConnectionParams.setConnectionTimeout(client.getParams(),
                        20000); // Timeout Limit
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0

Go to windows command prompt to know google ip address. type "ping www.google.com" You will get the ip of google and then use the ip instead of google web address. If the net connection is available and you have the right manifest permission then this step will must help you... Have fun

This is the code-

Use this ip instead of www.google.co.in, because emulator in local machine can't convert web address to corresponding ip. If you are using the machine which is connected to proxy server then it won't have this problem. And your device able to run this app. This ip conversion only for this emulator.

public static String GOOGLE_IP = "http://74.125.236.183";

public static String getContent(String url) throws Exception {
     return(new Scanner(new URL(url).openConnection().getInputStream()).useDelimiter("/z").next());
}
Suvam Roy
  • 1,282
  • 2
  • 11
  • 21