使用HttpClient获取网上字符串和位图对象Bitmap
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的更多相关文章
- 【荐】使用eval()、new Function()将JSON字符串转换为JSON对象
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...
- UTF-8编码的字符串拆分成单字、获取UTF-8字符串的字符个数的代码及原理
一.字符编码简介 1. ASCII码 在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(by ...
- JSON字符串和java对象的互转【json-lib】
在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级的数据格式比xml效率要高,XML需要很多的标签,这无疑占据了网络流量,JSON在这方面则做的很好, ...
- 根据字符串生成类---类的类型.self---根据字符串创建控制器对象
swift和OC一样,都是通过NSClassFromString,根据一个字符串,生成相应的类. // UITabBarButton是系统的私有类,不能直接使用 // if btn.isKind(of ...
- JSON字符串与java对象的转换
所需的jar包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar ...
- JSON 字符串 与 java 对象的转换
jsonLib 经典文章:http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html // 引入相应的包 //j ...
- Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mengzhengbin520.blog.51cto.com/7590564/12 ...
- wemall app商城源码中基于JAVA通过Http请求获取json字符串的代码
wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.分享其中关于通过Http请求获取json字符串的代码供 ...
- json字符串转json对象,json对象转换成java对象
@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...
随机推荐
- openjudge-膨胀的木棍
http://noi.openjudge.cn/ch0111/09/ 总时间限制: 1000ms 内存限制: 65536kB 描述 当长度为L的一根细木棍的温度升高n度,它会膨胀到新的长度L'=(1 ...
- CentOS源 Ubuntu 源 OpenSUSE-----持续汇总
CentOS 基础(常用)的源:http://dl.fedoraproject.org/pub/epel/epel-release-latest-5.noarch.rpmhttp://dl.fedor ...
- C++ 取得系统当前时间
#include <time.h> //* 方法一 time_t tt = time(NULL);//这句返回的只是一个时间cuo tm* t= localtime(&tt); p ...
- Ubuntu16.04+Tensorlow+caffe+opencv3.1+theano部署
1.首先安装Ubuntu16.04系统. 2.安装显卡驱动 在官网上下载最新的NVIDIA-Linux-x86_64-375.26.run驱动.然后 Ctrl+Alt+F1进入控制台,输入 sudo ...
- angular2开发01
// */ // ]]> angular2开发01 1. angular2 开发准备 1.1. 安装node 1.2. 安装npm 1.3. 运行qickStart 1 angular2 开发准 ...
- 提供VR定制开发、AR定制开发(VR游戏定制、应用定制)
设置输出路径 添加烘培输出的贴图类型 添加“LightingMap”类型 设置烘培贴图大小和目标贴图位置为“自发光” 设置烘培材质,选择“输出到源” 点击“渲染”即可 24.标准材质贴图的烘培光影处理 ...
- Cannot get a connection, pool exhausted解决办法
http://blog.163.com/it_message/blog/static/8892051200908102032653/ 连接池(Tomcat+oracle),运行一段时间后就会出现 Ca ...
- File存对象--android 的File存储到SD卡();
方法1:android File存对象--File存储到SD卡(); 1.保存对象到本地或SD卡需要注意的是,要保存的对象(OAuthV1)一定要实现了Serializable接口.实现了Serial ...
- Python单链表实现
class Node(): def __init__(self,InitDate): self.Date=InitDate self.next=None def setNext(self,newnex ...
- PHP常用功能
1.PHP字符串 字符串声明 变量=''或者""(一般情况会使用单引号,因为写起来会比较方便) $str = 'Hello PHP'; echo $str; strpos 计算字符 ...