packagecom.clw.drp.http;importjava.util.List;importorg.apache.http.HttpResponse;importorg.apache.http.HttpStatus;importorg.apache.http.NameValuePair;importorg.apache.http.client.HttpClient;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.params.CoreConnectionPNames;importorg.apache.http.protocol.HTTP;importorg.apache.http.util.EntityUtils;importandroid.os.Bundle;importandroid.os.Handler;importandroid.os.Message;importandroid.util.Log;public class HttpPostThread extendsThread {private static final String TAG=”HttpPostThread”;private Handler handle = null;
String url= null;
String token= null;
String contentInfo= null;
List paramList = null;//构造函数
publicHttpPostThread(Handler hander) {
handle=hander;
}/*** 启动线程*/
public void doStart(String url, String token, String contentInfo, ListparamList) {this.url =url;this.token =token;this.contentInfo =contentInfo;this.paramList =paramList;this.start();
}/*** 线程运行*/@Overridepublic voidrun() {super.run();
HttpClient httpClient= newDefaultHttpClient();
HttpPost httpPost= newHttpPost(url);
String response= “”;try{
httpPost.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);
httpPost.setHeader(“authorization”, “Bearer ” + this.token);if (null !=contentInfo) {
httpPost.setHeader(“Content-Type”, “application/json”);
httpPost.setEntity(newStringEntity(contentInfo, HTTP.UTF_8));
}else{
httpPost.setEntity(newUrlEncodedFormEntity(paramList, HTTP.UTF_8));
}
HttpResponse httpResponse=httpClient.execute(httpPost);
Log.i(TAG,”调用POST请求————————————“);int statusCode =httpResponse.getStatusLine().getStatusCode();if (statusCode ==HttpStatus.SC_OK) {
response=EntityUtils.toString(httpResponse.getEntity());
}else{
response= “返回码:” +statusCode;
}
}catch(Exception e) {
e.printStackTrace();
response= “timeOut”;
}
Bundle bundle= newBundle();
bundle.putString(“data”, response);
Message message=handle.obtainMessage();
message.setData(bundle);
handle.sendMessage(message);
}
}