zoukankan      html  css  js  c++  java
  • OkHttpClient简单封装

    一.接口

    public interface HttpListener {
        void onFinish(String reponse);
        void onError(Exception e);
    }

    二.OkHttpUtil

    public class OkHttpUtil {
    
        public static void AsyncGet(String url, final HttpListener listener){
            OkHttpClient http = new OkHttpClient();
            final Request request = new Request.Builder()
                    .url(url)
                    .build();
    
            Call call = http.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    listener.onError(e);
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    listener.onFinish(response.body().string());
                }
            });
        }
    }

    三.调用

    OkHttpUtil.AsyncGet("http://www.baidu.com", new HttpListener() {
                @Override
                public void onFinish(String reponse) {
                    Log.e("log", reponse);
                }
    
                @Override
                public void onError(Exception e) {
                    e.printStackTrace();
                }
            });
  • 相关阅读:
    Java 基础
    Mybatis
    Gateway
    Debug
    Nacos
    Debug
    Debug
    echars 折线图之一条线显示不同颜色,及拐点显示不同颜色
    捌月份生活随笔
    MyMatrix2022 64位版本下载 64bits Edition
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6758909.html
Copyright © 2011-2022 走看看