我得到了一个UnknownHostException

时间:2011-03-26 02:51:27

标签: android

我试图从pro android2运行一个示例项目,但我得到一个例外,并且不知道如何解决它。

以下是代码:

Purejava.java

package com.purejavaz;

import android.app.Activity;
import android.os.Bundle;

public class Purejava extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        TestHttpGet get = new TestHttpGet();
        try {
            get.executeHttpGet();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

TestHttpGet.java

package com.purejavaz;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class TestHttpGet {

    public void executeHttpGet() throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("http://code.google.com/android/"));
        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();
                }
            }
        }
    }
}

的AndroidManifest.xml

<code><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.purejavaz"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <permission android:name="android.permission.INTERNET"></permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Purejava"
                  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>
</manifest></code>

日志错误:

03-26 10:30:57.205: WARN/System.err(335): java.net.UnknownHostException: code.google.com
03-26 10:30:57.215: WARN/System.err(335):     at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
03-26 10:30:57.215: WARN/System.err(335):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
03-26 10:30:57.225: WARN/System.err(335):     at java.net.InetAddress.getAllByName(InetAddress.java:242)
03-26 10:30:57.225: WARN/System.err(335):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
03-26 10:30:57.225: WARN/System.err(335):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-26 10:30:57.235: WARN/System.err(335):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-26 10:30:57.245: WARN/System.err(335):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
03-26 10:30:57.245: WARN/System.err(335):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-26 10:30:57.245: WARN/System.err(335):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-26 10:30:57.245: WARN/System.err(335):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-26 10:30:57.255: WARN/System.err(335):     at com.purejavaz.TestHttpGet.executeHttpGet(TestHttpGet.java:20)
03-26 10:30:57.255: WARN/System.err(335):     at com.purejavaz.Purejava.onCreate(Purejava.java:13)
03-26 10:30:57.265: WARN/System.err(335):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-26 10:30:57.265: WARN/System.err(335):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-26 10:30:57.265: WARN/System.err(335):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-26 10:30:57.275: WARN/System.err(335):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-26 10:30:57.275: WARN/System.err(335):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-26 10:30:57.275: WARN/System.err(335):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 10:30:57.285: WARN/System.err(335):     at android.os.Looper.loop(Looper.java:123)
03-26 10:30:57.285: WARN/System.err(335):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-26 10:30:57.295: WARN/System.err(335):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 10:30:57.295: WARN/System.err(335):     at java.lang.reflect.Method.invoke(Method.java:521)
03-26 10:30:57.295: WARN/System.err(335):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-26 10:30:57.295: WARN/System.err(335):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-26 10:30:57.305: WARN/System.err(335):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案