Retrofit2.0 + Okhttp3.0获取接口原生数据

public interface ApiService {
        @Multipart
        @POST("/mm/xxx")
        Call<ResponseBody> uploadImage(@Part MultipartBody.Part file);

        @FormUrlEncoded
        @POST("/mm/xxx")
        Call<ResponseBody> post(@FieldMap TreeMap<String,String> map);
    }
public void test(){
        Retrofit build = new Retrofit.Builder()
                .baseUrl("http://140.28.115.161/")
                .build();

//        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), logFile);
//        MultipartBody.Part body = MultipartBody.Part.createFormData("uploaded_file", logFile.getName(), requestFile);
        TreeMap<String,String> treeMap = new TreeMap<>();
        treeMap.put("userId", "182683141");
        Call<ResponseBody> stringCall = build.create(ApiService.class).post(treeMap);
        stringCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                try {
                    ResponseBody body = response.body();
                    assert body != null;
                    // 这样就获取到接口返回的原生json字符串了
                    String trim = body.string().trim();
                    LogUtil.e("FileLogCatUtils"," trim = " + trim);
                }catch (Exception e){
                    e.printStackTrace();
                }
                LogUtil.e("FileLogCatUtils"," call = " + call + " response = " + response);
                LogUtil.e("FileLogCatUtils"," ThreadName = " + Thread.currentThread().getName());
            }

            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
                LogUtil.e("FileLogCatUtils"," call = " + call + " Throwable = " + t);
            }
        });
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值