Android: 如何使用okhttp3发送带有payload参数的PUT请求

Android: How to use okhttp3 to send a PUT request with payload parameters

我已经尝试了很多不同的方法来使用 okhttp3 发送带有负载的 PUT 请求,但是其中 none 可以工作,有人可以告诉我如何使用 okhttp3 来完成这项工作吗?我只是从 Chrome 浏览器粘贴了一些关键的 header 信息。

    General:
    Request URL:http://8.102.239.245/abc/api/108/actions?name=Rename
    Request Method:PUT

    Request Headers: 
    Content-Type:application/json;charset=UTF-8

    Query String Parameters: 
    name:Rename

    Request Payload: 
    {id: 108, name: "dbd"}

我建议也添加 Retrofit 以便于 API 定义:

compile 'com.squareup.retrofit2:retrofit:2.1.0'

添加后,执行以下操作:

  1. 定义接口和您的请求作为方法:

    public interface MyService {
        @PUT("abc/api/{id}/actions")
        Call<YourResponse> putName(@Path("id") int id, @Query("name") String name);
    }
    
  2. 使用 OkHttpClient 实例化 API:

     MyService myService = okHttpClient.create(MyService.class)
    
  3. 发出您的请求:

    Call<YourResponse> call = myService.putName(108, "Rename");
    call.enqueue(new Callback<YourResponse>() { ... };