java http put请求方式_Http四种请求方式:post ,get ,put,delete

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值