Android UnknownHost in asyncTask - loading web page
Posted
by
Sneha
on Stack Overflow
See other posts from Stack Overflow
or by Sneha
Published on 2012-03-23T06:16:41Z
Indexed on
2012/03/24
11:30 UTC
Read the original article
Hit count: 238
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
© Stack Overflow or respective owner