如何在改造中将 POST 变量作为查询参数
How to POST variable as query parameter in retrofit
//url to consume - baseURL/xyz/test-map.php?key=3ec9864e9db5fb47da338d108503d4612fc65b80
@FormUrlEncoded
@POST("xyz/test-map.php")
Call<Root> getResponseForApi (@Field(value = "key", encoded = true) String key ,
@Field("auth_key") int auth_key,
@Field("password") int password) ;
当我尝试打电话时,
Call<Root> call = apiServiceImplementation.getResponseForApi(key, auth_key, password);
call.enqueue(new Callback<Root>() {
@Override
public void onResponse(Call<Root> call, Response<Root> response) {
Root data = response.body();
}
@Override
public void onFailure(Call<Root> call, Throwable t) {
Log.e("TAG", "onFailure: " );
}
});
我在响应中收到 URL,因为没有像“baseURL/xyz/test-map.php
”这样的查询参数。
为什么它没有编码
您可以在请求中同时使用@Query 和@Field。
这是您的解决方案。
@FormUrlEncoded
@POST("xyz/test-map.php")
Call<Root> getResponseForApi (@Query("key") String key ,
@Field("auth_key") int auth_key,
@Field("password") int password) ;
//url to consume - baseURL/xyz/test-map.php?key=3ec9864e9db5fb47da338d108503d4612fc65b80
@FormUrlEncoded
@POST("xyz/test-map.php")
Call<Root> getResponseForApi (@Field(value = "key", encoded = true) String key ,
@Field("auth_key") int auth_key,
@Field("password") int password) ;
当我尝试打电话时,
Call<Root> call = apiServiceImplementation.getResponseForApi(key, auth_key, password);
call.enqueue(new Callback<Root>() {
@Override
public void onResponse(Call<Root> call, Response<Root> response) {
Root data = response.body();
}
@Override
public void onFailure(Call<Root> call, Throwable t) {
Log.e("TAG", "onFailure: " );
}
});
我在响应中收到 URL,因为没有像“baseURL/xyz/test-map.php
”这样的查询参数。
为什么它没有编码
您可以在请求中同时使用@Query 和@Field。 这是您的解决方案。
@FormUrlEncoded
@POST("xyz/test-map.php")
Call<Root> getResponseForApi (@Query("key") String key ,
@Field("auth_key") int auth_key,
@Field("password") int password) ;