• Http四种请求方式:post ,get ,put,delete


      一.POST

    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);
      }
    }
  • 相关阅读:
    未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.ServiceModel.Activation.HttpModule”。
    服务代理的关闭与并发会话的限制
    批量插入数据利器之SqlBulkCopy
    VS 生成 dll、exe 版本号与SVN版本号一致
    DataTable / DataSet 与 xml 的相互转换
    C# 操作嵌入的资源
    【WCF安全】使用X509证书自定义验证
    预先生事件和后期生成命令事件
    (转)利用WPF的ListView进行大数据量异步加载
    (转)SQL知识_SqlParameter数组
  • 原文地址:https://www.cnblogs.com/yico/p/5179091.html
Copyright © 2020-2023  润新知