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

关于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是在不断使用中发现的,同时,我自己遇到这个坑,也希望后面遇到这个坑的人不要浪费太多时间。

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值