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"% ...
随机推荐
- Oracle数据库合并行记录,WMSYS.WM_CONCAT 函數的用法
Sql代码 select t.rank, t.Name from t_menu_item t; 10 CLARK 10 KING 10 MILLER 20 ADAMS 20 F ...
- JavaScript_Math函数
JavaScript_Math函数与属性按功能分类 Math三角函数与属性 Math.sin() -- 返回数字的正弦值 Math.cos() -- 返回数字的余弦值 Math.tan() -- 返回 ...
- [LintCode] Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Have you met ...
- Android:The connection to adb is down, and a severe error has occured.解决方法一
在自己机上打安桌虚拟机,竟然提示“The connection to adb is down, and a severe error has occured.please ensure ......” ...
- jquery选择器实例说明
选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的元素 .class $( ...
- Javascript 编程小技巧总结(部分内容借鉴他人)
1 – 使用===,而不是== ==(或!=)操作符在需要的时候会自动执行类型转换.===(或!==)操作不会执行任何转换.它将比较值和类型,而且在速度上也被认为优于==. 2 – 使用闭包实现私有变 ...
- ZK 父窗口与子窗口消息交互
父窗口代码: 前台(test.zul) <?page title="" contentType="text/html;charset=UTF-8"?> ...
- 帝国备份王(Empirebak)万能cookie及拿shell
1.伪造cookie登录系统(其实这一步多余的,大多用户连密码都没改,都是默认的123456) 登录成功设置4个cookie,看代码 function login($lusername,$lpassw ...
- 微信APP支付(Java后台生成签名具体步骤)
public class PayCommonUtil { //定义签名,微信根据参数字段的ASCII码值进行排序 加密签名,故使用SortMap进行参数排序 public static String ...
- Java面试题大全(四)
JAVA代码查错 1. abstract class Name { private String name; public abstract boolean isStupidName(String n ...