我按照AsyncTask的本教程获取以下错误日志:
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)
我该如何解决?
我的代码:
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>
清单:
<?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>
感谢
斯纳
答案 0 :(得分:0)
在调用网络之前,只需添加以下两行:
try { InetAddress i = InetAddress.getByName("YourDesiredURL"); }
catch (UnknownHostException e1) { e1.printStackTrace(); }
您不需要捕获任何异常,只需在Web调用之前执行它。 有关详细信息,请阅读this
答案 1 :(得分:0)
尝试了你的代码并得到了同样的错误来解决错误, 我替换了两行代码
HttpGet httpGet = new HttpGet(url);
HttpResponse execute = client.execute(httpGet);
通过,
HttpPost httppost = new HttpPost(url);
HttpResponse execute = client.execute(httppost);
编辑:
在创建HttpClient
对象后添加此行。
HttpConnectionParams.setConnectionTimeout(client.getParams(),
20000); // Timeout Limit
答案 2 :(得分:0)
转到Windows命令提示符以了解google ip地址。 输入“ping www.google.com” 您将获得谷歌的IP,然后使用IP而不是谷歌网址。 如果网络连接可用并且您拥有正确的清单权限,那么此步骤必须帮助您... 玩得开心
这是代码 -
使用此IP而不是www.google.co.in,因为本地计算机中的模拟器无法将网址转换为相应的IP。如果您使用连接到代理服务器的计算机,则它不会出现此问题。并且您的设备能够运行此应用程序。 此ip转换仅适用于此模拟器。
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());
}