zoukankan      html  css  js  c++  java
  • OKHttpUtil工具类

     导入jar包下载链接 http://square.github.io/okhttp/

    package com.common.util;
     
    import java.io.IOException;
     
    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
     
    /**
     * 利用okhttp进行get和post的访问
     * 
     * @author cp
     *
     */
    public class OKHttpUtil {
     
        /**
         * get请求
         * @param url
         * @return
         */
        public static String httpGet(String url) {
            String result = null;
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(url).build();
            try {
                Response response = client.newCall(request).execute();
                result = response.body().string();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
     
        /**
         * post请求
         * @param url
         * @param data  提交的参数为key=value&key1=value1的形式
         */
        public static String httpPost(String url, String data) {
            String result = null;
            OkHttpClient httpClient = new OkHttpClient();
            RequestBody requestBody = RequestBody.create(MediaType.parse("text/html;charset=utf-8"), data);
            Request request = new Request.Builder().url(url).post(requestBody).build();
            try {
                Response response = httpClient.newCall(request).execute();
                result = response.body().string();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
    }
    1. package com.handkoo.util;
    2.  
       
    3.  
      import java.io.IOException;
    4.  
       
    5.  
      import okhttp3.MediaType;
    6.  
      import okhttp3.OkHttpClient;
    7.  
      import okhttp3.Request;
    8.  
      import okhttp3.RequestBody;
    9.  
      import okhttp3.Response;
    10.  
       
    11.  
      /**
    12.  
      * 利用okhttp进行get和post的访问
    13.  
      *
    14.  
      * @author cp
    15.  
      *
    16.  
      */
    17.  
      public class OKHttpUtil {
    18.  
       
    19.  
      /**
    20.  
      * 发起get请求
    21.  
      *
    22.  
      * @param url
    23.  
      * @return
    24.  
      */
    25.  
      public static String httpGet(String url) {
    26.  
      String result = null;
    27.  
      OkHttpClient client = new OkHttpClient();
    28.  
      Request request = new Request.Builder().url(url).build();
    29.  
      try {
    30.  
      Response response = client.newCall(request).execute();
    31.  
      result = response.body().string();
    32.  
      } catch (Exception e) {
    33.  
      e.printStackTrace();
    34.  
      }
    35.  
      return result;
    36.  
      }
    37.  
       
    38.  
      /**
    39.  
      * 发送httppost请求
    40.  
      *
    41.  
      * @param url
    42.  
      * @param data 提交的参数为key=value&key1=value1的形式
    43.  
      * @return
    44.  
      */
    45.  
      public static String httpPost(String url, String data) {
    46.  
      String result = null;
    47.  
      OkHttpClient httpClient = new OkHttpClient();
    48.  
      RequestBody requestBody = RequestBody.create(MediaType.parse("text/html;charset=utf-8"), data);
    49.  
      Request request = new Request.Builder().url(url).post(requestBody).build();
    50.  
      try {
    51.  
      Response response = httpClient.newCall(request).execute();
    52.  
      result = response.body().string();
    53.  
      } catch (IOException e) {
    54.  
      e.printStackTrace();
    55.  
      }
    56.  
      return result;
    57.  
      }
    58.  
      }
  • 相关阅读:
    centos7 安装redis服务及phpredis扩展
    centos7 LANMP 安装
    php reids 单机命令
    浅谈正向代理和反向代理
    php-fpm日志告警"seem busy"
    PHP-redis中文文档
    Nginx配置文件nginx.conf中文详解
    生成带参数的微信二维码
    微信临时二维码时为32位非0整型
    [Linux]Service mysql start出错(mysql: unrecognized service)解决方法
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/9566311.html
Copyright © 2011-2022 走看看