JSON网络数据传输的公共类

来源:互联网 发布:淘宝网登陆页 编辑:程序博客网 时间:2024/04/30 08:33
最近在项目里面 看到一个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 blocke.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 为 HttpClientHttpClient 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 exceptione.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();}}


0 0