我使用IOException
从我的代码中获得AndroidHttpClient
。代码有问题吗?无论我使用port 80
还是port 443
,还是使用Http架构http
或https
,都会抛出代码。
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)
{
}
答案 0 :(得分:1)
您是否在应用中声明了互联网权限?
您需要将以下行添加到AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />