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



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

    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>() { ... };
    

相关内容

最新更新