AndroidHttpClient问题

时间:2011-12-06 14:08:41

标签: java android http

我使用IOException从我的代码中获得AndroidHttpClient。代码有问题吗?无论我使用port 80还是port 443,还是使用Http架构httphttps,都会抛出代码。

client.execute抛出了异常......这是一个UnknownHostException异常......我不知道为什么。我可以从浏览器点击该服务。

        // declare locals
    JSONObject joResponse = new JSONObject();
    String response = "";

    try
    {
        // prepare to execute
        HttpHost target = new HttpHost(hostname, Const.HTTP_PORT, Const.HTTP_SCHEME);
        HttpGet get = new HttpGet();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        AndroidHttpClient client = AndroidHttpClient.newInstance(null);

        // execute
        response = client.execute(target, get, responseHandler); // this is where the exception is thrown

        joResponse = new JSONObject(response);
    }
    catch (ClientProtocolException e)
    {
    }
    catch (IOException e)
    {
    }
    catch(Exception e)
    {
    }

1 个答案:

答案 0 :(得分:1)

您是否在应用中声明了互联网权限?

您需要将以下行添加到AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />