JSON网络数据传输的公共类

最近在项目里面 看到一个JSON数据传输提取出来的公公类,非常方便,以后 都可以直接拿来用,里面的注释很多很明白。这个类的详细情况,Request:一个是GET请求,一个是POST请求,Response是String类型,按规则对返回来的String做数据解析,下面是代码:
package com.ku_wan.mb.http;


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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.ku_wan.mb.commons.GlobalVariate;
import com.ku_wan.mb.ui.HomePageActivity;
import com.ku_wan.mb.ui.LoginActivity;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

/**
 * 
 * @author Ku_wan
 *
 */
public class HttpDownload {
	
	private static HttpClient httpClient =null;
	
	public static HttpClient getHttpClient(){
		if(httpClient == null){
			httpClient = new DefaultHttpClient();
		}
		return httpClient;
	}
	
	public static String getJSONData(String url)
			throws ClientProtocolException, IOException {
		String result = "";
		HttpGet httpGet = new HttpGet(url);
		HttpClient httpClient = getHttpClient();
		HttpResponse httpResponse = null;

		try {
			httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			if (httpEntity != null) {
				InputStream inputStream = httpEntity.getContent();
				result = convertStreamToString(inputStream);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			throw e;
		} finally {
			httpClient.getConnectionManager().shutdown();
			httpResponse = null;
		}
		return result;

	}
	
	public static String sendPostHttpRequest(String url,List<NameValuePair> params){
		//设置HttpPost连接对象(HttpPost 即为 HttpRequest)
		HttpPost httpPost = new HttpPost(GlobalVariate.SERVERADDRESS+url);
//		params.add(new BasicNameValuePair("userId", Storage.getString(context, "userId")));
		try {
			//设置字符集
			HttpEntity httpEntity = new UrlEncodedFormEntity(params,"utf-8");
			//
			httpPost.setEntity(httpEntity);
			//使用DefaultHttpClient 为 HttpClient
			HttpClient httpClient = getHttpClient();
			//取得HttpResponse 对象
			HttpResponse httpResponse = httpClient.execute(httpPost);
			//HttpStatus.SC_OK 表示连接成功
			if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				//取得返回的字符串
				String result ="";
				if(httpResponse.getEntity() != null){
					InputStream is = httpResponse.getEntity().getContent();
					result = convertStreamToString(is);
//					result = result.replaceAll("\r", "");
					return result.trim();
				}
			}else {
				System.out.println("fail the request");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),// 防止模拟器上的乱码
					512 * 1024);
		} catch (UnsupportedEncodingException e1) {

			e1.printStackTrace();
		}
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			Log.e("DataProvier convertStreamToString", e.getLocalizedMessage(),e);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("HttpDownload::::"+sb.toString());//HttpDownload返回的结果
		return sb.toString();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值