okhttp 发送get post 请求

package com.qlwb.business.util;

import java.util.Map;

import com.alibaba.fastjson.JSON;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class UrlConnectionUtil {

public static String post(String url, Map<String, String> paramsMap) { // 这里没有返回,也可以返回string
OkHttpClient mOkHttpClient = new OkHttpClient();
String resStr = null;

try {
MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");
RequestBody requestbody = RequestBody.create(mediaType, JSON.toJSONString(paramsMap));
Request request = new Request.Builder().post(requestbody).url(url).build();

Response response = mOkHttpClient.newCall(request).execute();
resStr = response.body().string();
} catch (Exception e) {
e.printStackTrace();
return resStr;
}

return resStr;

}
}

posted on 2019-05-31 14:59  四六之间  阅读(5692)  评论(0编辑  收藏  举报