package com.iexin.skd4s.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class HttpConnectionUtils implements Runnable {
public static String SESSION_ID;
private static final String TAG = HttpConnectionUtils.class.getSimpleName();
public static final int DID_START = 0;
public static final int DID_ERROR = 1;
public static final int DID_SUCCEED = 2;
private static final int GET = 0;
private static final int POST = 1;
private static final int PUT = 2;
private static final int DELETE = 3;
private static final int BITMAP = 4;
private static final int CONN_TIME_OUT = 5;
private static final int READ_TIME_OUT = 6;
private static final int NO_NETWORKE = 7;
private static final int NO_LOGIN = 8;
private String url;
private int method;
private Handler handler;
private List<NameValuePair> data;
private HttpClient httpClient;
public HttpConnectionUtils() {
this(new Handler());
}
public HttpConnectionUtils(Handler _handler) {
handler = _handler;
}
public void create(int method, String url, List<NameValuePair> data) {
Log.d(TAG, "method:"+method+" ,url:"+url+" ,data:"+data);
this.method = method;
this.url = url;
this.data = data;
ConnectionManager.getInstance().push(this);
}
public void get(String url) {
create(GET, url, null);
}
public void post(String url, List<NameValuePair> data) {
create(POST, url, data);
}
public void put(String url, List<NameValuePair> data) {
create(PUT, url, data);
}
public void delete(String url) {
create(DELETE, url, null);
}
public void bitmap(String url) {
create(BITMAP, url, null);
}
@Override
public void run() {
handler.sendMessage(Message.obtain(handler, HttpConnectionUtils.DID_START));
httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 6000);
try {
HttpResponse response = null;
switch (method) {
case GET:
HttpGet httpGet = new HttpGet(url);
if(SESSION_ID != null){
httpGet.setHeader("Cookie", "JSESSIONID=" + SESSION_ID);
}
response = httpClient.execute(httpGet);
break;
case POST:
HttpPost httpPost = new HttpPost(url);
if(SESSION_ID != null){
httpPost.setHeader("Cookie", "JSESSIONID=" + SESSION_ID);
}
httpPost.setEntity(new UrlEncodedFormEntity(data,HTTP.UTF_8));
response = httpClient.execute(httpPost);
break;
case PUT:
HttpPut httpPut = new HttpPut(url);
if(SESSION_ID != null){
httpPut.setHeader("Cookie", "JSESSIONID=" + SESSION_ID);
}
httpPut.setEntity(new UrlEncodedFormEntity(data,HTTP.UTF_8));
response = httpClient.execute(httpPut);
break;
case DELETE:
HttpDelete httpDelete = new HttpDelete(url);
if(SESSION_ID != null){
httpDelete.setHeader("Cookie", "JSESSIONID=" + SESSION_ID);
}
response = httpClient.execute(httpDelete);
break;
case BITMAP: //下载图片
HttpGet httpGetBit = new HttpGet(url);
if(SESSION_ID != null){
httpGetBit.setHeader("Cookie", "JSESSIONID=" + SESSION_ID);
}
response = httpClient.execute(httpGetBit);
processBitmapEntity(response.getEntity());
break;
}
if (method < BITMAP) {
processEntity(response.getEntity());
}
setSessionId(response);
} catch (Exception e) {
handler.sendMessage(Message.obtain(handler,HttpConnectionUtils.DID_ERROR,e));
}
ConnectionManager.getInstance().didComplete(this);
}
/**
* 设置网络请求的sessionId
* @param response
* 服务器放回的数据
*/
private static void setSessionId(HttpResponse response) {
Header[] h = response.getHeaders("Set-Cookie");
for (Header header : h) {
String session = header.toString();
String sessionPair = session.substring(session.indexOf(":") + 1,
session.indexOf(";"));
SESSION_ID = sessionPair.substring(sessionPair.indexOf("=") + 1);
break;
}
}
private void processEntity(HttpEntity entity) throws IllegalStateException,
IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(entity
.getContent()));
String line, result = "";
while ((line = br.readLine()) != null)
result += line;
Message message = Message.obtain(handler, DID_SUCCEED, result); //发送响应数据到UI线程
handler.sendMessage(message);
}
private void processBitmapEntity(HttpEntity entity) throws IOException {
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm));//发送下载到的图片到UI线程
}
public static String getUrlFromResources(Context context,int sURL) {
return context.getString(sURL);
}
public static String getUrlFromResources(Context context,String sURL) {
return sURL;
}
}
- 1
- 2
前往页