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;


    }


}

猜你喜欢

转载自www.cnblogs.com/yangchaojie/p/9207207.html