Android网络工具

在android开发中,我们常常需要连接到远程的服务器进行数据的读取,常用的是Http协议从服务器读取信息。以下代码是我在开发Android项目的时候写的一个连接工具,封装了get和post两种请求数据的方式。

package com.zhang.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;

import android.content.Context;

import com.zhang.exception.ExceptionHandle;
import com.zhang.exception.NullPointException;

public class NetUtil {
	private HttpClient httpClient = new DefaultHttpClient();
	private Context context;
	private String charSet = "UTF-8";

	public NetUtil(Context context) {
		this.context = context;

	}

	public String sendByGet(String url, List<ParameterEntity> parameters,boolean cacheable) throws ClientProtocolException, IOException {
		String result = null;
		if(cacheable == true){
			//从缓存中读取数据
		}else{
			if (url != null) {
				HttpUriRequest request = new HttpGet(url);
				if(parameters != null){
					BasicHttpParams params = new BasicHttpParams();
					for (ParameterEntity entity : parameters) {
						params.setParameter(entity.getKey(), entity.getValue());
					}
					request.setParams(params);
				}
				HttpResponse response = httpClient.execute(request);
				result = getContent(response);
			} else {
				ExceptionHandle.handle(context, new NullPointException());
			}
		}
		return result;
	}
	
	private String getContent(HttpResponse response) throws IllegalStateException, IOException{
		String result = null;
		if(response != null){
			HttpEntity httpEntity = response.getEntity();
			InputStream in = httpEntity.getContent();
			InputStreamReader reader = new InputStreamReader(in,charSet);
			BufferedReader bread = new BufferedReader(reader);
			char[] buf = new char[1024];
			StringBuffer buffer = new StringBuffer();
			int count = 0;
			while((count = bread.read(buf))!= -1){
				buffer.append(buf,0,count);
			}
			result = buffer.toString();
		}
		return result;
	}

	public String sendByPost(String url,List<ParameterEntity> parameters, boolean cacheable) throws ClientProtocolException, IOException {
		String result = null;
		if(cacheable == true){
			//从缓存中读取数据
		}else{
			HttpPost post = new HttpPost(url);
			post.setHeader("Accept-Charset", charSet);
			if(parameters != null){
				BasicHttpParams params = new BasicHttpParams();
				for (ParameterEntity entity : parameters) {
					params.setParameter(entity.getKey(), entity.getValue());
				}
				post.setParams(params);
			}
			HttpResponse response = httpClient.execute(post);
			result = getContent(response);
		}
		return result;
	}

}


转载于:https://my.oschina.net/zhanghongbin01/blog/547716

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值