android http post请求获取 json 图片

public class SuperAsyncHttp {
	Context context;
	/**
	 * 单例模式
	 */
	private static SuperAsyncHttp superAsyncHttp=new SuperAsyncHttp();
	private SuperAsyncHttp(){}
	public static SuperAsyncHttp getInstance(){
		if(superAsyncHttp==null){
			superAsyncHttp=new SuperAsyncHttp();
		}
		return superAsyncHttp;
	}

	/**
	 * 异步http请求下载图片返回Drawable对象
	 */
	public Drawable post4Drawable(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 post4Bitmap(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;
	}
	/**
	 * 异步http请求获取Json类型的返回值
	 * @return
	 */
	public JSON request4Json(Context c,JSON json){
		context=c;
		AsyncHttpClient client=new AsyncHttpClient();
		try{
			StringEntity stringEntity = new StringEntity(json.toString());
			client.post(context, "http://192.4.200.29/testxr/Home/TestReq", stringEntity, "application/json", new JsonHttpResponseHandler(){

				@Override
				public void onSuccess(JSONObject response) {
					super.onSuccess(response);
					if(response.toString()!=null){
						Toast.makeText(context, response.toString(), 1000).show();
						Log.i("response", response.toString());
					}else{
						Log.i("response", "请求错误");
					}
				}
			});
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}

//格式转换类

public class FormatTools {
	private static FormatTools tools = new FormatTools();

	private FormatTools() {
	}

	public static FormatTools getInstance() {
		if (tools == null) {
			tools = new FormatTools();
			return tools;
		}
		return tools;
	}

	// 将byte[]转换成InputStream
	public InputStream Byte2InputStream(byte[] b) {
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		return bais;
	}

	// 将InputStream转换成byte[]
	public byte[] InputStream2Bytes(InputStream is) {
		String str = "";
		byte[] readByte = new byte[1024];
		int readCount = -1;
		try {
			while ((readCount = is.read(readByte, 0, 1024)) != -1) {
				str += new String(readByte).trim();
			}
			return str.getBytes();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 将Bitmap转换成InputStream
	public InputStream Bitmap2InputStream(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		InputStream is = new ByteArrayInputStream(baos.toByteArray());
		return is;
	}

	// 将Bitmap转换成InputStream
	public InputStream Bitmap2InputStream(Bitmap bm, int quality) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
		InputStream is = new ByteArrayInputStream(baos.toByteArray());
		return is;
	}

	// 将InputStream转换成Bitmap
	public Bitmap InputStream2Bitmap(InputStream is) {
		return BitmapFactory.decodeStream(is);
	}

	// Drawable转换成InputStream
	public InputStream Drawable2InputStream(Drawable d) {
		Bitmap bitmap = this.drawable2Bitmap(d);
		return this.Bitmap2InputStream(bitmap);
	}

	// InputStream转换成Drawable
	public Drawable InputStream2Drawable(InputStream is) {
		Bitmap bitmap = this.InputStream2Bitmap(is);
		return this.bitmap2Drawable(bitmap);
	}

	// Drawable转换成byte[]
	public byte[] Drawable2Bytes(Drawable d) {
		Bitmap bitmap = this.drawable2Bitmap(d);
		return this.Bitmap2Bytes(bitmap);
	}

	// byte[]转换成Drawable
	public Drawable Bytes2Drawable(byte[] b) {
		Bitmap bitmap = this.Bytes2Bitmap(b);
		return this.bitmap2Drawable(bitmap);
	}

	// Bitmap转换成byte[]
	public byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}

	// byte[]转换成Bitmap
	public Bitmap Bytes2Bitmap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		}
		return null;
	}

	// Drawable转换成Bitmap
	public Bitmap drawable2Bitmap(Drawable drawable) {
		Bitmap bitmap = Bitmap
				.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}

	// Bitmap转换成Drawable
	public Drawable bitmap2Drawable(Bitmap bitmap) {
		BitmapDrawable bd = new BitmapDrawable(bitmap);
		Drawable d = (Drawable) bd;
		return d;
	}
}


转载于:https://my.oschina.net/547217475/blog/209859

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值