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);

 

转载于:https://www.cnblogs.com/rongguang/p/4822534.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值