使用retrofit框架

普通get请求 

    @GET("service/getIpInfo.php")

    Call<String> getIpInfo(@Query("ip") String ip);

参数在body内的post请求
    @Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头
    @POST("omcApp-web/order/queryOrderList.do?sign=b5f56819de1316305d29d848d89d14e0")
    Call<String> getResponseByPostBody(@Body RequestBody route);

参数以键值对的post请求
    @POST("mobileLogin")
    Call<String> getResponseByPostMaps(@QueryMap Map<String,Object> map);

图片上传
    @Multipart
    @POST("medical-API/uploadFile/")
    Call<String> getUploadImage(@Part("file\"; filename=\"image.jpg")RequestBody imgs);

WebService请求
    @Headers({"Content-Type: text/xml","SOAPAction: url"})
    @POST("SMS_Service.asmx?op=method")

    Call<String> getWeatherbyCityName(@Body String  requestEnvelope);


//====================以上是关于网络请求注解的api==================================

Map<String, Object> map = new HashMap<>(); 

map.put("params","params");

ApiService apiService = initNetWorkConfig().create(ApiService.class);

Call<String> call = apiService.getIpInfo("192.xx.xx.xx");

//postbody

RequestBody body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

//post图片

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

//postxml(webServices)

RequestBody body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), xmlParams);

//map转xml

 public String getMapToXml(Map<String,String> map,String methName){
        String head = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n";
        String ends = "</soap:Envelope>";
        StringBuffer stringBuffer = new StringBuffer(head);
        stringBuffer.append("<soap:Body>\n");
        stringBuffer.append("<" + methName + " xmlns=\"http://tempuri.org/\">\n");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            stringBuffer.append("<" + entry.getKey() + ">" + entry.getValue() + "</" + entry.getKey() + ">\n");
        }
        stringBuffer.append("</" + methName + ">\n");
        stringBuffer.append("</soap:Body>\n");
        stringBuffer.append(ends);
        return stringBuffer.toString();
    }

====================================以上是传参=====================================

 call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
            //成功回执
            }


            @Override
            public void onFailure(Call<String> call, Throwable t) {
                //失败回执
            }
        });


猜你喜欢

转载自blog.csdn.net/zhujie_/article/details/52184550
今日推荐