Okhttp携带请求头发送JSON数据
public void send(){
new Thread(new Runnable() {
@Override
public void run() {
try{
OkHttpClient client = new OkHttpClient();
String url = "http://";
MediaType mediaType = MediaType.parse("application/json");
JSONObject Json = new JSONObject().put("键","值");
RequestBody requestBody = RequestBody.create(mediaType,Json.toString()+"");
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.addHeader("请求头名称","值")
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
JSONObject message = new JSONObject(responseData);
}catch (Exception e){e.printStackTrace();}
}
}).start();
}