HttpClient请求返回JSON、图片
/**
* Created by RongGuang on 2015/9/19.
*/
public class RongHttp {
/**
* Http Post请求
* @param url
* @param pairs 参数
* @param clzz 对象
* @return Object 返回相应对象
* @throws Exception
*/
public Object post(String url, List<NameValuePair> pairs,Class clzz) throws Exception {
String json;
//可通过配置文件获取
String host="http://fanyi.youdao.com/";
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.socket.timeout",3000);
client.getParams().setParameter("http.connection.timeout",3000);
client.getParams().setParameter("http.connection-manager.timeout",60*60L);
HttpPost httpPost = new HttpPost(host + url);
httpPost.setHeader("User-Agent", "RongGuang Of Android");
httpPost.setHeader("userId", "RongGuang");
try {
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "utf-8"));
try {
HttpResponse httpResponse = client.execute(httpPost);
json = EntityUtils.toString(httpResponse.getEntity());
System.out.println(">>$"+httpPost.getURI());
} catch (IOException e) {
throw new Exception(e);
}
} catch (UnsupportedEncodingException e) {
throw new Exception(e);
}
return JSON.parseObject(json,clzz);
} /**
* Http get请求
* @param url
* @param pairs
* @param clzz
* @return
* @throws Exception
*/
public Object get(String url, List<NameValuePair> pairs,Class clzz) throws Exception {
String json;
// AppOption appOption = new AppOption();
String host="http://fanyi.youdao.com/";
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.socket.timeout",3000);
client.getParams().setParameter("http.connection.timeout",3000);
client.getParams().setParameter("http.connection-manager.timeout", 60 * 60L);
String param= URLEncodedUtils.format(pairs, "UTF-8");
HttpGet httpGet = new HttpGet(host + url+"?"+param);
httpGet.setHeader("User-Agent", "RongGuang Of Android");
httpGet.setHeader("userId", "RongGuang"); try {
// Log.i("d======", host + url + "?" + param);
HttpResponse httpResponse = client.execute(httpGet);
json = EntityUtils.toString(httpResponse.getEntity());
// System.out.println(">>$=================================="+json);
} catch (IOException e) {
throw new Exception(e);
}
return JSON.parseObject(json, clzz);
}
/**
* 异步http请求下载图片返回Drawable对象
*/
public Drawable post2Drawable(String url){
HttpPost httpPost=null;
HttpClient httpClient=null;
HttpResponse httpResponse=null;
try{
httpPost=new HttpPost(url);
httpClient=new DefaultHttpClient();
httpResponse=httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){
InputStream is=httpResponse.getEntity().getContent();
return FormatTools.getInstance().InputStream2Drawable(is);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 异步http请求下载图片返回Bitmap对象
*/
public Bitmap post2Bitmap(String url){
HttpPost httpPost=null;
HttpClient httpClient=null;
HttpResponse httpResponse=null;
try{
httpPost=new HttpPost(url);
httpClient=new DefaultHttpClient();
httpResponse=httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200){
InputStream is=httpResponse.getEntity().getContent();
return FormatTools.getInstance().InputStream2Bitmap(is);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
直接相应的位置调用即可。
例:(谨记:主线程中不能访问网络、子线程中不能更新UI)
Bitmap bitmap=(SuperAsyncHttp.getInstance().post4Bitmap("http://img1.gtimg.com/news/pics/hv1/39/111/1927/125331519.jpg"));
iv.setImageBitmap(bitmap);
HttpClient请求返回JSON、图片的更多相关文章
- 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法
在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...
- shiro异步请求返回JSON响应
shiro异步请求返回JSON响应 需求1:当shiro请求资源,但是没有进行认证时,默认是进行重定向,现在需要返回JSON响应.注意异步请求,服务器重定向后,ajax拿到的是浏览器重定向后的到的页面 ...
- AJAX请求,返回json进行页面绑值
AJAX请求,返回json进行页面绑值 后台 controller @RequestMapping(value = "backjson.do",method=RequestMeth ...
- spring 4.x下让http请求返回json串
当前很多应用已经开始将响应返回为json串,所以基于springframework框架开发的服务端程序,让响应返回json字符串成为了一种常用手段. 这里介绍一下如何在spring-MVC框架下方便快 ...
- ajax请求返回json字符串/json对象 处理
1. 返回json字符串如何处理 $.ajax({ url:xxx, success:function(date){ }, error:function(){ } }); 通过最原始的返回: Prin ...
- springmvc通过ajax异步请求返回json格式数据
jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...
- 使用jQuery发送POST,Ajax请求返回JSON格式数据
问题: 使用jQuery POST提交数据到PHP文件, PHP返回的json_encode后的数组数据,但jQuery接收到的数据不能解析为JSON对象,而是字符串{"code" ...
- android通过httpClient请求获取JSON数据并且解析
使用.net创建一个ashx文件,并response.write json格式 public void ProcessRequest(HttpContext context) { context.R ...
- jsp Ajax请求(返回json数据类型)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
随机推荐
- [LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- [LintCode] Identical Binary Tree 相同二叉树
Check if two binary trees are identical. Identical means the two binary trees have the same structur ...
- Hibernate中易错地方的总结
1.Hibernate中的配置文件要放在src下,注意不能放在包目录下 2.Hibernate中@Before @After方法不能再普通的类里用,只有在专门的JUnit测试用例里面用. 3.使用 ...
- eclipse中如何修改dynamic web module version
java项目中,若切换服务器,经常会涉及到动态web模块版本的问题. 比如:新建了web项目,开始使用tomcat服务器,但是后来使用jboss服务器,就会出现:Project facet ...
- Java 正则表达式[转载]
PS:转载自CSDN博客看上去很美 众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学 ...
- LNMP环境简易安装流程
1.关闭防火墙 [root@CentOS ~]# chkconfig iptables off 2.关闭selinux vi /etc/sysconfig/selinux //将SELINUX=enf ...
- 15.2 THE USE OF A LARGE REGISTER FILE
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION
- 2014年国人开发的最热门的开源软件TOP 100
不知道从什么时候开始,很多一说起国产好像就非常愤慨,其实大可不必.做开源中国六年有余,这六年时间国内的开源蓬勃发展,从一开始的使用到贡献,到推出自己很多的开源软件,而且还有很多软件被国外的认可.中国是 ...
- 3D touch的 使用心得
一.设置图标touch 快捷进入 1.静态标签 静态标签是我们在项目的配置plist文件中配置的标签,在用户安装程序后就可以使用,并且排序会在动态标签的前面. 我们先来看静态标签的配置: 首先,在in ...
- 《linux内核设计与实现》读书笔记第三章
第3章 进程管理 3.1 进程 1.进程 进程就是处于执行期的程序. 进程包括: 可执行程序代码 打开的文件 挂起的信号 内核内部数据 处理器状态 一个或多个具有内存映射的内存地址空间 一个或多个执行 ...