使用HttpClient获取网上字符串和位图对象Bitmap

时间:2022-09-20 16:47:36
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. Cisco交换机设置管理IP

    需要准备一根CONSOLE线和带串行接口的电脑. (图1) 用CONSOLE线连接好电脑与交换机(交换机的CONSOLE口一般都有表示). 然后按照图1点“开始→程序→超级终端”会弹出来一个窗口(图2 ...

  2. 免写前缀JS包--prefixfree.min.js--插件

    /** * StyleFix 1.0.3 & PrefixFree 1.0.7 * @author Lea Verou * MIT license */ (function(){functio ...

  3. 网络流初步——增广路算法(EK)模板

    #include <iostream> #include <queue> #include<string.h> using namespace std; #defi ...

  4. ubuntu搭建 zabbix3&period;2 with mysql database (Ubuntu 14&period;04&period;5 LTS)

    官网文档 服务构建:https://www.zabbix.com/documentation/3.2/manual/installation/install_from_packages/server_ ...

  5. 关于flex的布局理解

    flex布局是一种新的布局方式也就是弹性盒子:在布局上更加方便,但是要注意的是在flex布局中float.position是无效的. 图片来自阮一峰的博客 .下面我们进入正题: flex布局分为分为主 ...

  6. gradle测试出现IllegalArgumentException

    今天clone了一份代码,跑gradle test时出现failed,从report上来看是这个错误:IllegalArgumentException,具体如下: java.lang.IllegalA ...

  7. rhel5&period;4&plus;oracle 10g rac

    各种报错各种愁啊 ... 1> 不知道什么原因,在节点2执行root.sh 报错 .无解 . 还原虚拟机,重新安装 .唯一与以前不同的是,执行orainroot.sh后 接着在节点2执行.再去分 ...

  8. preg&lowbar;replace正则表达式的使用

    <?php $str="as2223adfsf0s4df0sdfsdf"; echo preg_replace("/0/","",$s ...

  9. 使用shell数据处理数据实例①-------手把手教学版

    引子: 在工作过程中经常要处理各种小数据,同事间会用各种工具方法来处理,比如用java.python.Perl甚至用UE手工处理.但貌似不都方便. 今天举一例子使用shell来处理,来说明shell一 ...

  10. 解决 pycharm can not save setting

    这个问题出现的原因是因为PyCharm中存在相同名字的虚拟环境变量. 解决方法:Configure Python Interpreter 点击右上角齿轮状按钮, 选择 show all,然后删除相同名 ...