您的位置:首页 > 编程语言 > Java开发

关于java.net.UnknownHostException: Unable to resolve host "xx": No address associated with hostname

2016-03-16 11:57 603 查看
关于java.net.UnknownHostException: Unable to resolve host “xx”: No address associated with hostname

前段时间在使用Asynchttpclient这个网络请求框架的时候,遇到过标题的这个错误,翻墙找了挺久,终于解决了,下面贴出解决办法。

1.我的需求是POST Json数据到服务器,然后返回Json数据(下面是Asynchttpclient post json的方法)

AsyncHttpClient client=new AsyncHttpClient();

client.setTimeout(10000);

client.addHeader(“content-type”,”application/json;charset=UTF-8”);

//方法1:使用Gson把实体类转成Json

// Gson gson=new Gson();

// String object = gson.toJson(new HttpEntity());

// Log.i(TAG,object);

StringEntity stringEntity = null;

try {

//方法2: 自己手写Json

JSONObject jsonObject=new JSONObject();

JSONObject jsonHeader=new JSONObject();

jsonHeader.put(“code”,”R1002”);

JSONObject jsonBody=new JSONObject();

jsonBody.put(“companyCode”,”beiqixinnengyuan”);

jsonBody.put(“username”,”ryan”);

jsonBody.put(“imei”,”86130402042171”);

jsonObject.put(“body”, jsonBody);

jsonObject.put(“header”, jsonHeader);

Log.i(TAG,jsonObject.toString());

stringEntity = new StringEntity(jsonObject.toString());

stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,

“application/json;charset=UTF-8”));

}catch (Exception e){

e.printStackTrace();

}

client.post(getApplicationContext(), URL, stringEntity, “application/json;charset=UTF-8”,new JsonHttpResponseHandler(){

//重写成功和失败的方法

});

2.然后写完就提示我java.net.UnknownHostException: Unable to resolve host “xx”: No address associated with hostname;于是百度了一下,百度给了几个答案(Manifest权限,是否联网等)都解决不了我的问题,然后翻墙去谷歌了一把, Asynchttpclient的github托管地址里有人提出是因为使用最新版的asynchttpclient-1.4.9.jar的问题,然后我下了个1.4.4的jar试了一下,果然是jar包的问题。

3.这个bug给我提了一个醒,所谓的开源,就是大家都能去共享这个东西,也希望这个东西可以做的更好,所以bug是在不断使用中发现的,同时,我自己遇到这个坑,也希望后面遇到这个坑的人不要浪费太多时间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: