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

      1 package test;
      2 
      3 import java.io.File;
      4 import java.io.IOException;
      5 import java.util.ArrayList;
      6 import java.util.List;
      7 
      8 import javax.management.RuntimeErrorException;
      9 
     10 import okhttp3.Call;
     11 import okhttp3.Callback;
     12 import okhttp3.FormBody;
     13 import okhttp3.MediaType;
     14 import okhttp3.OkHttpClient;
     15 import okhttp3.Request;
     16 import okhttp3.RequestBody;
     17 import okhttp3.Response;
     18 
     19 /**
     20  * 发送http请求的工具类
     21  */
     22 public class HttpUtil {
     23 
     24     //放置请求的URL
     25     public class URL {
     26 
     27         public static final String REMOTE_ADDRESS = "http://47.94.166.158:8080/android_server/";
     28         //登陆
     29         public static final String LOGIN = REMOTE_ADDRESS + "user_login";
     30         //。。。。
     31     }
     32 
     33     /**
     34      * GET请求,不传参只获取结果
     35      * @param url         url地址
     36      * @param callback     回调函数
     37      */
     38     public static void doGet(String url, Callback callback) throws IOException {
     39         //创建okHttpClient对象
     40         OkHttpClient mOkHttpClient = new OkHttpClient();
     41         //创建一个Request
     42         Request request = new Request.Builder()
     43         .url(url)
     44         .build();
     45 
     46         //new call
     47         Call call = mOkHttpClient.newCall(request);
     48         //请求加入调度
     49         call.enqueue(callback);
     50     }
     51 
     52     /**
     53      * 通用post请求(同步)
     54      * @param url             请求地址
     55      * @param paramNames    表单属性
     56      * @param paramValues    表单值
     57      * @throws IOException 
     58      */
     59     public static String doPost(String url, String[] paramNames, String[] paramValues) throws IOException {
     60 
     61         OkHttpClient client = new OkHttpClient();
     62 
     63         FormBody.Builder builder = new FormBody.Builder();
     64         for (int i = 0; i < paramNames.length; i++) { 
     65             builder.add(paramNames[i], paramValues[i]); 
     66         }
     67 
     68         Request request = new Request.Builder()
     69         .url(url)
     70         .post(builder.build())
     71         .build();
     72 
     73         Call call = client.newCall(request);
     74         return call.execute().body().string();
     75     }
     76 
     77     /**
     78      * 通用post请求(异步)
     79      * @param url             请求地址
     80      * @param paramNames    表单属性
     81      * @param paramValues    表单值
     82      * @param callback         回调函数
     83      */
     84     public static void doPostAsync(String url, String[] paramNames, String[] paramValues, Callback callback) {
     85 
     86         OkHttpClient client = new OkHttpClient();
     87 
     88         FormBody.Builder builder = new FormBody.Builder();
     89         for (int i = 0; i < paramNames.length; i++) { builder.add(paramNames[i], paramValues[i]); }
     90 
     91         Request request = new Request.Builder()
     92         .url(url)
     93         .post(builder.build())
     94         .build();
     95 
     96         Call call = client.newCall(request);
     97         call.enqueue(callback);
     98     }
     99 
    100     /**
    101      * 文件上传(上传头像,视频)
    102      * @param url            请求地址
    103      * @param paramValues    上传的文件,头像为1个,视频可以同时上传多个
    104      */
    105     public static void fileUpload(final String url, final File file) {
    106 
    107         OkHttpClient client = new OkHttpClient();
    108 
    109         //文件类型
    110         MediaType TYPE_FILE = MediaType.parse("application/octet-stream");
    111 
    112         Request.Builder builder = new Request.Builder();
    113 
    114         for (int i = 0; i < paramValues.length; i++) {
    115 
    116             builder.post(RequestBody.create(TYPE_FILE, paramValues[i]));
    117         }
    118 
    119         Request request = builder.url(url)
    120                 .build();
    121 
    122         Call call  = client.newCall(request);
    123         call.enqueue(callback);
    124 
    125     }
    126 
    127     /**
    128      * 表单参数和文件同时请求(异步)
    129      * @param url             请求地址
    130      * @param paramNames    属性名
    131      * @param paramValues    属性值
    132      * @param callback         回调函数
    133      */
    134     
    135     
    136 }
  • 相关阅读:
    Sqli-labs Less-47 order by后的注入
    Sqli-labs Less-46 order by后的注入
    Sqli-labs Background-9 order by后的injection
    Sqli-labs Less-45 堆叠注入
    jsp,servlet知识点
    jsp页面编码不统一可能会出问题
    jsp页面找不到,jsp页面乱码
    BZOJ 2843: 极地旅行社 lct splay
    2018/3/23 省选模拟赛
    bzoj 4573: [Zjoi2016]大森林 lct splay
  • 原文地址:https://www.cnblogs.com/webyyq/p/8616321.html
Copyright © 2011-2022 走看看