为什么在这种情况下未知主机?

时间:2012-01-05 08:14:03

标签: android http-get

这是我连接到URL的HTTP Get方法:

public void executeHttpGet() throws Exception {
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://test.mobibon.com.tw/MovieGoTest/GetMovies.js");
        HttpResponse response = client.execute(request);
        String retSrc = EntityUtils.toString(response.getEntity());

        Log.i("bird",retSrc);
        } finally {
        }
    }

以下是我的称呼方式:

    try {
        executeHttpGet();
    } catch (Exception e) {
        Log.i("bird","nothing");
        e.printStackTrace();
    }

http://test.mobibon.com.tw/MovieGoTest/GetMovies.js< -----此网址在我的浏览器中正常运行

但是Exception消息是:UnknownHostException

为什么?

3 个答案:

答案 0 :(得分:0)

检查xml中是否存在的权限: <uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:0)

您的AVD可能没有互联网连接。

关闭您的AVD并将其从CMD重新打开为E:\android-sdk-windows\tools>emulator -avd mj -dns-server 8.8.8.8

答案 2 :(得分:0)

你好检查这个功能

 private InputStream retriveInputStream(String url){
    DefaultHttpClient client=new DefaultHttpClient();
    HttpGet getRequest=new HttpGet(url);

    try{

        HttpResponse getResponse=client.execute(getRequest);
        final int statusCode=getResponse.getStatusLine().getStatusCode();
        if(statusCode!=HttpStatus.SC_OK){
            Log.w(getClass().getSimpleName(),"Error " + statusCode + " for URL " + url);
        }
        HttpEntity getResponseEntity=getResponse.getEntity();
        return getResponseEntity.getContent();
    }catch(IOException ioe){
        ioe.printStackTrace();
    }

    return null;
}

请参阅此链接以了解Android中的JSON Parse Click Here