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"% ...
随机推荐
- CSS3两个动画顺序衔接播放
问题描述: 第一个动画先播放,播放完成后,第二个动画紧接着播放. 解决办法: 1. 将第二个的延迟时间(animation-delay) 设置成第一个的持续时间( animation-duration ...
- Objective-c的内存管理MRC与ARC
Objective-c的内存管理MRC与ARC Objective-c中提供了两种内存管理机制MRC(MannulReference Counting)和ARC(Automatic Referen ...
- Trie字典树 静态内存
静态字典树 看了好久的字典树,挺简单的一个结构,愣是看了这么久才写出来... 专心一点就不会这样了.... 接下来就去刷刷字典树的题吧....... 下面是字典树.... 定义节点 typedef s ...
- wamp虚拟主机的配置 .
开发环境:WAMP 实例一,Apaceh配置localhost虚拟主机步骤 1,用记事本打开apache目录下httpd文件(如:D:\wamp\bin\apache\apache2.2.8\conf ...
- linux笔记七---------管道
smarty的变量调节器就是linux的管道 管道:前者的输出是后者的输入 {$name|upper} 通过调节器使得名字变为大写输出 {$name|lower} linux的管道: ls –al ...
- js 获取控制台的错误信息
适合移动端调试的时候优化的报错使用.缺点是无法打印出多条记录,有方法的同学留言告诉我. <!DOCTYPE html> <html lang="en"> & ...
- service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误
service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误的方法如下: [root@ctohome.com ~]# service ...
- spidermark sensepostdata ntp_monlist.py
试NTP 时间服务器用的,ntp_ip_enum.py,源码如下:#!/usr/bin/env python"""Basic script to pull address ...
- php课程---随机数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Solved Unable to copy the source file ./installer/services.sh to the destination file /etc/vmware-t
Sometimes when you intall vmwaretools there will be some problems such as "Unable to copy the s ...