zoukankan      html  css  js  c++  java
  • [技术博客]OKhttp3使用get,post,delete,patch四种请求

    OKhttp3使用get,post,delete,patch四种请求

    1.okhttp简介

    okhttp封装了大量http操作,大大简化了安卓网络请求操作,是现在最火的安卓端轻量级网络框架。如今okhttp已经更新到了okhttp4.0, 支持Android5.0以及以上的版本,要求Java在8.0以及以上的版本。

    2.okhttp安装

    • 可以通过添加依赖进行安装

      implementation("com.squareup.okhttp3:okhttp:4.7.2")
      
    • 可以通过JAR的方式进行安装,

      https://github.com/square/okhttp/
      
    • 如果使用的是androidStudio可以在Project Structure--->Dependencies 点击“+”号选Library dependency在搜索页面分别搜okttp,okio

    3.okhttp使用

    3.1get请求

    这里提供官方给的例子

    OkHttpClient client = new OkHttpClient();
    
    String run(String url) throws IOException {
      Request request = new Request.Builder()
          .url(url)
          .build();
    
      try (Response response = client.newCall(request).execute()) {
        return response.body().string();
      }
    }
    

    3.2POST请求

    同样提供官方给的例子

    ublic static final MediaType JSON
        = MediaType.get("application/json; charset=utf-8");
    
    OkHttpClient client = new OkHttpClient();
    
    String post(String url, String json) throws IOException {
      RequestBody body = RequestBody.create(json, JSON);
      Request request = new Request.Builder()
          .url(url)
          .post(body)
          .build();
      try (Response response = client.newCall(request).execute()) {
        return response.body().string();
      }
    }
    

    3.3delete请求

    public class DeleteApi {
        private OkHttpClient client;
        public void Delete(final Handler handler, final String url, final int what){
            final String token = TokenPool.getTokenPool().UserToken;
            client = new OkHttpClient();
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    try {
                        String result = getUrl(url,token);
                        //    Log.d("TAG",result);
                        Message message1 = Message.obtain();
                        message1.what=what;
                        message1.obj = result;
                        handler.sendMessage(message1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
            }.start();
        }
        String getUrl(String url,String token) throws IOException {
            FormBody body = new FormBody.Builder().build();
            Request request = new Request.Builder()
                    .url(url)
                    .delete(body)
                    .addHeader("Authorization","Bearer "+token)
                    .build();
    
            try (Response response = client.newCall(request).execute()) {
                return response.body().string();
            }
        }
    }
    

    3.4patch请求

    public class PatchApi {
        private OkHttpClient client;
        private MediaType mediaType
                = MediaType.parse("application/json; charset=utf-8");
        public void patch(final Handler handler,final String url,final RequestBody body, final int what){
            final String token = TokenPool.getTokenPool().UserToken;
            client = new OkHttpClient();
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    try {
                        String result = getUrl(url,body,token);
                        Message message1 = Message.obtain();
                        message1.what= what;
                        message1.obj = result;
                        handler.sendMessage(message1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
            }.start();
        }
        String getUrl(String url, RequestBody body, String token) throws IOException {
            Request request = new Request.Builder()
                    .url(url)
                    .addHeader("Authorization","Bearer "+token)
                    .patch(body)
                    .build();
    
            try (Response response = client.newCall(request).execute()) {
                return response.body().string();
            }
        }
    }
    
  • 相关阅读:
    LeetCode206翻转链表问题,多解法求解
    使用GCC编译c源程序经历的几个步骤
    部分内置函数(不含面向对象型)
    Python初学1
    函数的作用域与匿名函数
    生成器函数以及生产消费模型
    【VC编译错误】error C2872: 'ofstream' : ambiguous symbol
    【C开发】无限循环 while(1) 和 for(; ;)
    【C开发】预编译处理命令(#define、typedef、#include、#运算符)
    编译DLL出现无法解析的外部符号
  • 原文地址:https://www.cnblogs.com/miokun/p/12972961.html
Copyright © 2011-2022 走看看