u269069 5 lat temu
rodzic
commit
e0c179ed34
2 zmienionych plików z 139 dodań i 0 usunięć
  1. 68 0
      interUtil/HttpClientUtils.java
  2. 71 0
      interUtil/SyncUrils.java

+ 68 - 0
interUtil/HttpClientUtils.java

@@ -0,0 +1,68 @@
+package com.easemob.easeui.interUtil;
+
+import android.util.Log;
+
+import org.apache.http.HttpResponse;
+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.client.utils.URLEncodedUtils;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+
+import java.util.List;
+
+
+public class HttpClientUtils {
+
+	public static String get(String baseUrl,List<BasicNameValuePair> params ) {
+
+		// 对参数编码
+		String param = URLEncodedUtils.format(params, "UTF-8");
+		// 将URL与参数拼接  发送请求
+		HttpGet getMethod = new HttpGet(baseUrl + "?" + param);
+		//创建默认客户端的实例
+		HttpClient httpClient = new DefaultHttpClient();
+		try {
+			//执行get请求返回 响应对象
+			HttpResponse response = httpClient.execute(getMethod); // 发起GET请求
+			//获取响应状态码并判断
+			if(response.getStatusLine().getStatusCode() == 200){
+				System.out.println("连接成功");
+				return EntityUtils.toString(response.getEntity(),"utf-8");
+
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		}
+
+		return null;
+	}
+
+	public static String post(String baseUrl,List<BasicNameValuePair> params){
+		try {
+
+			HttpPost postMethod = new HttpPost(baseUrl);
+			//设置参数的类型
+			postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中 
+
+			HttpClient httpClient = new DefaultHttpClient();
+
+			HttpResponse response = httpClient.execute(postMethod); //执行POST方法 
+
+			if(response.getStatusLine().getStatusCode() == 200){
+				return EntityUtils.toString(response.getEntity(),"utf-8");
+
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		}
+
+		return null;
+	}
+
+}

+ 71 - 0
interUtil/SyncUrils.java

@@ -0,0 +1,71 @@
+package com.easemob.easeui.interUtil;
+
+import android.os.AsyncTask;
+import org.apache.http.message.BasicNameValuePair;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+/*
+ * 异步线程工具类
+ */
+public class SyncUrils extends AsyncTask<Object, Void, String> {
+
+	private String url;
+	private List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
+	private Map<String,String> mapParm;
+	private String http_request_tag = GET;
+	public static final String POST = "POST";
+	public static final String GET = "GET";
+	private OnResponseListener onResponseListener;
+
+	public void setOnResponseListener(OnResponseListener onResponseListener) {
+		this.onResponseListener = onResponseListener;
+	}
+
+	public SyncUrils(String url,Map<String,String> mapParm,String request_ype){
+		this.mapParm = mapParm;
+		this.url = url;
+		this.http_request_tag = request_ype;
+	}
+
+	@Override
+	protected String doInBackground(Object... arg0) {//用于判断后台耗时操作
+		String result = null;
+		params.clear();
+		if(this.url == null || this.params == null){
+			return result;
+		}
+		Set<Entry<String, String>> entrySet = mapParm.entrySet();//把map集合转换为set
+		for(Entry<String, String> entryset : entrySet ){//把key和value值添加到
+			String key = entryset.getKey();
+			String value = entryset.getValue();
+			BasicNameValuePair bName = new BasicNameValuePair(key, value);
+			params.add(bName);
+		}
+		if(http_request_tag.equals("GET")){
+			result =  HttpClientUtils.get(url, params);
+		}else if(http_request_tag.equals("POST")){
+			result =  HttpClientUtils.post(url, params);
+		}
+		return result;
+	}
+
+	@Override
+	protected void onPostExecute(String result) {//后台任务执行完以后调用
+		if(this.onResponseListener != null){
+			if(result != null){
+				this.onResponseListener.sussce(result);
+			}else{
+				this.onResponseListener.error(result);
+			}
+		}
+	}
+
+	public interface OnResponseListener {
+		public void sussce(Object obj);
+		public void error(Object... obj);
+	}
+}