关于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是在不断使用中发现的,同时,我自己遇到这个坑,也希望后面遇到这个坑的人不要浪费太多时间。
前段时间在使用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是在不断使用中发现的,同时,我自己遇到这个坑,也希望后面遇到这个坑的人不要浪费太多时间。
相关文章推荐
- 如何修改struts上传文件大小
- Java中synchronized与java.util.concurrent.locks.Lock区别
- SessionFactory中getCurrentSession地址的对比
- Java并发编程:线程池的使用
- Java NIO系列教程(八) SocketChannel
- 使用RxJava实现EventBus
- java 比较两个二叉树是否相等
- 使用spring mvc返回JSON,chrome可以,firefox不行的问题定位
- Struts2 Convention Plugin ( struts2 零配置 )
- Java关键字final、static使用总结
- Jdk5.0中出现的新特性
- Java数组,去掉重复值、增加、删除数组元素
- Java NIO系列教程(七) FileChannel
- JAVA 中BIO,NIO,AIO的理解
- Spring整合activiti-modeler5.16遇到的小问题
- MyBatis与Spring设置callSettersOnNulls
- Spring整合activiti-modeler5.16遇到的小问题
- Java运算符优先级
- Weblogic操作数据源NameNotFoundException解决办法
- eclipse安装jrebel控件及破解方法,maven项目的热部署