zoukankan      html  css  js  c++  java
  • Ok-Http | Android 网络请求框架使用方式

      POST :

    package he3.sd.util;
    
    
    import com.parkingwang.okhttp3.LogInterceptor.LogInterceptor;
    
    import java.io.IOException;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    import okhttp3.FormBody;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    
    
    /**
     * 请求微信小程序数据
     *
     * @author YangChaoJie
     */
    public class OkHttpUtil {
        //全局,节省内存,官方推荐。
        private static OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new LogInterceptor()).build();
        //动态添加参数
        static FormBody.Builder params;
    
        static Response response;
    
        public static Response sendPostRequest(String url, Object parameter) throws IOException {
    
            LinkedHashMap<String, Object> map = ReqUtil.ParameToMap(parameter);
    
            params = new FormBody.Builder();
            //遍历参数,KV对应。
            for (Map.Entry<String, Object> mapping : map.entrySet()) {
                params.add(mapping.getKey(), (String) mapping.getValue());
            }
    
            Request request = new Request.Builder()
                    .url(url)
                    .post(params.build())
                    .build();
    
            response = okHttpClient.newCall(request).execute();
    
            return response;
    
    
        }
    
    
    }
  • 相关阅读:
    LocalDate、LocalTime、LocalDateTime示例
    Instant时间戳示例
    Mybatis面试题
    SpringMVC面试题
    Spring面试题
    redis面试题
    计算机网络面试题
    java集合面试题
    java基础面试题
    MySQL面试题汇总
  • 原文地址:https://www.cnblogs.com/yangchaojie/p/9207207.html
Copyright © 2011-2022 走看看