import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory; public class HttpUtils { private static InputStream is, is2;
private static ByteArrayOutputStream baos,baos2; public static String getJSONStr(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
is = entity.getContent();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int i;
while ((i = is.read(buffer)) != -1) {
baos.write(buffer, 0, i);
}
return baos.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} public static Bitmap getBitmap(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url); try {
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
is2 = entity.getContent();
baos2 = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int i;
while ((i = is2.read(buffer)) != -1) {
baos2.write(buffer, 0, i);
}
Bitmap bm = BitmapFactory.decodeByteArray(baos2.toByteArray(), 0, baos2.toByteArray().length);
return bm;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} }

使用HttpClient获取网上字符串和位图对象Bitmap的更多相关文章

  1. 【荐】使用eval()、new Function()将JSON字符串转换为JSON对象

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...

  2. UTF-8编码的字符串拆分成单字、获取UTF-8字符串的字符个数的代码及原理

    一.字符编码简介 1. ASCII码 在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(by ...

  3. JSON字符串和java对象的互转【json-lib】

    在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级的数据格式比xml效率要高,XML需要很多的标签,这无疑占据了网络流量,JSON在这方面则做的很好, ...

  4. 根据字符串生成类---类的类型.self---根据字符串创建控制器对象

    swift和OC一样,都是通过NSClassFromString,根据一个字符串,生成相应的类. // UITabBarButton是系统的私有类,不能直接使用 // if btn.isKind(of ...

  5. JSON字符串与java对象的转换

    所需的jar包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar ...

  6. JSON 字符串 与 java 对象的转换

    jsonLib 经典文章:http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html // 引入相应的包 //j ...

  7. Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mengzhengbin520.blog.51cto.com/7590564/12 ...

  8. wemall app商城源码中基于JAVA通过Http请求获取json字符串的代码

    wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.分享其中关于通过Http请求获取json字符串的代码供 ...

  9. json字符串转json对象,json对象转换成java对象

    @RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...

随机推荐

  1. C#遍历指定文件夹中的所有文件和子文件夹

    参考:http://www.cnblogs.com/skylaugh/archive/2012/09/23/2698850.html DirectoryInfo TheFolder=new Direc ...

  2. Does Lamda expression return value?

    Basically, the compiler does this for you. If you write a lambda as a single statement (and don't in ...

  3. JAVA访问权限

      同一个类 同一个包 不同包的子类 不同包的非子类 Private √       Default √ √     Protected √ √ √   Public √ √ √ √

  4. Arrays和Collection之间的转换

    1.将数组转换成固定大小的列表: public static <T> List<T> asList(T... a)参数 T... a:数组元素 返回值 List<T> ...

  5. Dojo特效(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/effects/index.html In this tutorial, we will exp ...

  6. Use Visual studio 2010 build Python2.7.10

    http://p-nand-q.com/python/building-python-27-with-vs2010.html

  7. 自实现CAS原理JAVA版,模拟下单库存扣减

    在做电商系统时,库存是一个非常严格的数据,根据CAS(check and swap)原来下面对库存扣减提供两种方法,一种是redis,一种用java实现CAS. 第一种 redis实现: 以下这个类是 ...

  8. 读写hdfs文件(工作笔记)

    import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; ...

  9. [MySQL] SqlServer 迁移到 MySQL 方法介绍

    一.原则: 只迁移表结构和数据,存储过程.函数.触发器尽量自己改写,并充分测试. 迁移前,先设置好数据库的一些参数,比如默认存储引擎,默认编码等,方便后续导入. 二.方法: 1.使用MySQL Wor ...

  10. js产生随机数函数

    函数: //产生随机数函数 function RndNum(n){ var rnd=""; for(var i=0;i<n;i++) rnd+=Math.floor(Math ...