• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
浅时光
我喜欢,驾驭着代码在风驰电掣中创造完美!我喜欢,操纵着代码在随必所欲中体验生活!我喜欢,书写着代码在时代浪潮中完成经典!每一段新的代码在我手中诞生对我来说就象观看刹那花开的感动!
博客园    首页    新随笔    联系   管理    订阅  订阅

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

 

posted @ 2015-09-19 23:12  浅时光  阅读(2090)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3