Http四种请求方式:post ,get ,put,delete
2016-02-03 10:13
756 查看
一.POST
二.GET
三.PUT
四.DELETE
package com.clw.drp.http; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; public class HttpPostThread extends Thread { private static final String TAG="HttpPostThread"; private Handler handle = null; String url = null; String token = null; String contentInfo = null; List<NameValuePair> paramList = null; // 构造函数 public HttpPostThread(Handler hander) { handle = hander; } /** * 启动线程 */ public void doStart(String url, String token, String contentInfo, List<NameValuePair> paramList) { this.url = url; this.token = token; this.contentInfo = contentInfo; this.paramList = paramList; this.start(); } /** * 线程运行 */ @Override public void run() { super.run(); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(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(new StringEntity(contentInfo, HTTP.UTF_8)); } else { httpPost.setEntity(new UrlEncodedFormEntity(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 = new Bundle(); bundle.putString("data", response); Message message = handle.obtainMessage(); message.setData(bundle); handle.sendMessage(message); } }
二.GET
package com.clw.drp.http; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class HttpGetThread extends Thread { private Handler handle = null; String url = null; String token = null; // 构造函数 public HttpGetThread(Handler hander) { handle = hander; } /** * 启动线程 */ public void doStart(String url, String token) { this.url = url; this.token = token; this.start(); } /** * 线程运行 */ @Override public void run() { super.run(); System.out.println("--------------------调用get请求"); String response = null; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse; try { httpGet.setHeader("authorization", "Bearer " + this.token); httpResponse = httpClient.execute(httpGet); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { response = EntityUtils.toString(httpResponse.getEntity()); } else { response = "返回码:" + statusCode; } } catch (ClientProtocolException e) { e.printStackTrace(); response = "timeOut"; } catch (IOException e) { e.printStackTrace(); response = "timeOut"; } Bundle bundle = new Bundle(); bundle.putString("data", response); Message message = handle.obtainMessage(); message.setData(bundle); handle.sendMessage(message); } }
三.PUT
package com.clw.drp.http; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class HttpPutThread extends Thread { @SuppressWarnings("unused") private static final String TAG = "HttpPutThread"; private Handler handler = null; String url = null; String token = null; String contentInfo = null; List<NameValuePair> paramList = null; public HttpPutThread(Handler handler) { this.handler = handler; } /** * 启动线程 */ public void doStart(String url, String token, String contentInfo, List<NameValuePair> paramList) { this.url = url; this.token = token; this.contentInfo = contentInfo; this.paramList = paramList; this.start(); } @Override public void run() { super.run(); String response = null; HttpClient httpClient = new DefaultHttpClient(); HttpPut httpPut = new HttpPut(url); try { httpPut.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); httpPut.setHeader("authorization", "Bearer " + this.token); if (null != contentInfo) { httpPut.setHeader("Content-Type", "application/json"); httpPut.setEntity(new StringEntity(contentInfo, HTTP.UTF_8)); } else { httpPut.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8)); } HttpResponse httpResponse = httpClient.execute(httpPut); 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 = new Bundle(); bundle.putString("data", response); Message message = handler.obtainMessage(); message.setData(bundle); handler.sendMessage(message); } }
四.DELETE
package com.clw.drp.http; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class HttpDeleteThread extends Thread { @SuppressWarnings("unused") private static final String TAG = "HttpDeleteThread"; private Handler handle = null; String url = null; String token = null; // 构造函数 public HttpDeleteThread(Handler hander) { handle = hander; } /** * 启动线程 */ public void doStart(String url, String token) { this.url = url; this.token = token; this.start(); } @Override public void run() { super.run(); String response = null; HttpClient client = new DefaultHttpClient(); HttpDelete delete = new HttpDelete(url); HttpResponse httpResponse; try { delete.setHeader("authorization", "Bearer " + this.token); httpResponse = client.execute(delete); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { response = EntityUtils.toString(httpResponse.getEntity()); } else { response = "返回码:" + statusCode; } } catch (ClientProtocolException e) { e.printStackTrace(); response = "timeOut"; } catch (IOException e) { e.printStackTrace(); response = "timeOut"; } Bundle bundle = new Bundle(); bundle.putString("data", response); Message message = handle.obtainMessage(); message.setData(bundle); handle.sendMessage(message); } }
相关文章推荐
- HTTP 2.0的那些事
- c# http下载文件的代码
- 网络系统调用延迟
- openstack neutron gre网络拓扑实验总结
- WAMP配置httpd.conf允许外部访问
- IIS虚拟目录实现与文件服务器网络驱动器映射共享
- 使用Reachability类判断iOS设备的当前网络连接类型
- http错误代码含义大全详解
- 2016太原网络营销师郭文军剖析支付宝#集褔#的原因
- 【AJAX】——XMLHttpRequest对象
- xp系统下网络打印机怎么设置
- 构建LAMP平台(三)(软件版本:httpd-2.4.16,php-5.6.12,mysql-5.6.26)
- Android实现网络多线程断点续传下载
- 解决Win10服务主机本地系统网络受限
- Android 本地加载网页与显示网络图片
- 阿里再次打响社交网络之战?
- 给服务器免费的ssl证书 http和https共存
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包
- 计算机网络3--计算机网络结构
- 解密:华为的敏捷网络是SDN吗