String json = gson.toJson(book); //MediaType 设置Content-Type 标头中包含的媒体类型值 RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , json); Request request = new Request.Builder() .url("http://172.20.192.168:8080/getbookByJson")//请求的url...
ResponseBody data=response.body(); String json=data.toString(); processData(json); 上面的data.toString应该改为data.string()。 原因: data.toString返回的是json对象,data.string()返回的才是json数据。 上图: data.toString返回数据 data.string()返回数据...
varjson ="application/json; charset=utf-8".toMediaTypeOrNull(); val requestBody= RequestBody.create(json,Gson().toJson(PostBean(Account("13386050182","123456"))) val request=Request.Builder() .url(BASE_URL+"tony/accounts/login") .addHeader("Content-Type","application/json") .post(reques...
Gson gson = new Gson(); String obj = gson.toJson(dailyDetailAdapterItemBeanList); RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj); 然后retrofit2里面增加方法即可传递 //@FormUrlEncoded - @Body标签不能同时和@FormUrlEncoded、@Multipart标签...
然后retrofit2里面增加方法即可传递 //@FormUrlEncoded - @Body标签不能同时和@FormUrlEncoded、@Multipart标签同时使用@POST///< POST方法-Json - 获取数据【对象】Observable<HttpResponse<String>>postData(@UrlString url,@BodyRequestBody info); 然后干就完事了......
Response代表一个Http的响应,这个类的实例不可修改。 一个简单的Get请求和说明就结束了 2.4、POST 2.4.1、POST提交字符串 public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); ...
(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(json, JSON); Request request = new Request.Builder() .url(url) .post(body) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); }...
String params = gson.toJson(paramsMap); RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), params); String requestUrl = BASE_URL + url; final Request request = new Request.Builder() .url(requestUrl) ...
2、然后新建一个请求,这里用JsonObjectRequest(JsonArrayRequest同理),(接口这里用mockaroo和Mocky在线生成一个) JsonObjectRequest jsonObjectRequest = new JsonObjectRequest('http://www./v2/56c9d8c9110000c62f4e0bb0', null, new Response.Listener() { @Override public void onResponse(JSONObject response...
addParamJson("json")//添加Json参数 .addParamFile(new File(""))//添加文档参数 .addParamForm("form")//添加表单参数 .addDownloadFile(new DownloadFileInfo("fileURL", "myMP4",null))//添加下载文件 .addUploadFile("interfaceParamName","filePathWithName",null)//添加上传文件 .setResponseEncoding(...