前提:网络框架Retrofit
1、参数以json形式
@POSTfun requestPost(@Url url: String,@QueryMap map: Map<String, String>,@Body body: Map<String, String>): Observable<ResponseBody>
2、参数form表单形式,请求参数放到body里
@FormUrlEncoded@POSTfun requestPost(@Url url: String,@QueryMap map: Map<String, String>, @FieldMap body: Map<String, String>): Observable<ResponseBody>
前提:okhttp网络框架
okhttp3.Request okHttpRequest = obtainRequest(method, req);Call newCall = client.newCall(okHttpRequest);private okhttp3.Request obtainRequest(int method, Request req) {okhttp3.Request.Builder builder = new okhttp3.Request.Builder()switch (method) {case MethodStatus.TYPE_POST_JSON:return builder.url(req.getUrl()).post(jsonBody(req)).build();case MethodStatus.TYPE_POST_FORM:return builder.url(req.getUrl()).post(formBody(req)).build();case MethodStatus.TYPE_GET:return builder.url(requestUrl(req)).build();}}
1、参数以json形式
private RequestBody jsonBody(Request req) {MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json");String postBody = req.getJsonEncodedParams();RequestBody body = RequestBody.create(postBody, MEDIA_TYPE_JSON);return body;}
2、参数form表单形式,请求参数放到body里
private RequestBody formBody(Request req) {ContentValues params = req.getParams();//创建一个FormBody.BuilderFormBody.Builder builder = new FormBody.Builder();for (String key : params.keySet()) { //追加表单信息builder.add(key, String.valueOf(params.get(key)));}RequestBody body = builder.build();return body;}