zoukankan      html  css  js  c++  java
  • okHttp3的util类

    package com.sanro.common.util;
    
    import okhttp3.*;
    import org.springframework.stereotype.Component;
    
    import java.io.IOException;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author LingSong <19039339@cnsuning.com>
     * @date 2019/11/5 17:46
     */
    @Component
    public class OkHttpUtil {
    
        public String sendPost(String url, Map<String, String> map) {
            String responseBody = "";
            OkHttpClient okHttpClient = new OkHttpClient().newBuilder().connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(120, TimeUnit.SECONDS).build();
            FormBody.Builder builder = new FormBody.Builder();
            for(Map.Entry<String,String> entry : map.entrySet()){
                builder.add(entry.getKey(),entry.getValue());
            }
            RequestBody body = builder.build();
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            Call call = okHttpClient.newCall(request);
            try {
                Response response = call.execute();
                responseBody = response.body().string();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return responseBody;
        }
    
        public String sendAsynPost(String reqUrl, String body) {
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(reqUrl)
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    System.out.println("异步线程Id为:" + Thread.currentThread().getId());
                }
            });
    
            return "";
        }
    }
  • 相关阅读:
    Javascript图片预加载详解
    Canvas入门(3):图像处理和绘制文字
    CSS强制英文、中文换行与不换行 强制英文换行
    数组分隔成两个一组
    scrollview嵌套tableview
    审核被拒:1. 1 Safety: Objectionable Content ;3. 1.1 Business: Payments
    流程控制-用布尔值
    xcode代码提示没了
    sourceTree回退撤销commit
    iOS使用mask切割不规则图案
  • 原文地址:https://www.cnblogs.com/yoyotl/p/12246708.html
Copyright © 2011-2022 走看看