gpt4 book ai didi

android - 发送参数和主体行空改造

转载 作者:行者123 更新时间:2023-11-29 23:40:02 25 4
gpt4 key购买 nike

我正在尝试使用 Retrofit2 Android 库调用一项服务,该服务需要:

  • 授权
  • 标题
  • 参数
  • 正文

我的界面是这样的:

    @Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@FormUrlEncoded
@POST("/nameOfAction")
Call<ItemResponse> callAction(@Header("Authorization") String authorization, @Field("nameOfParameter") String nameOfParameter);

我用这段代码调用它:

 ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

Call<CarrelloResponse> call = apiService.callAction( "Bearer " + tokenAccess, "valueOfParameter");

call.enqueue(new Callback<ItemResponse>() {
@Override
public void onResponse(Call<ItemResponse> call, Response<ItemResponse> response) {

int statusCode = response.code();

if (statusCode == 200) {
if (response.isSuccessful()) {
// code
} else {
Log.e(TAG, response.errorBody().toString());
}
}
}

@Override
public void onFailure(Call<ItemResponse> call, Throwable t) {
Log.e(TAG, t.toString());

}
});

但它不起作用,因为我还需要发送 body raw empty 才能工作。

如果我用 Postman 发送它,如下图所示:

enter image description here

如有任何建议,我们将不胜感激。

最佳答案

尝试删除 @FormUrlEncoded 并将 @Field 更改为 @Query 参数,如下所示

@Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@POST("/nameOfAction")
Call<ItemResponse> callAction(@Header("Authorization") String authorization, @Query("nameOfParameter") String nameOfParameter);

******** 由 OP 编辑​​ *******

在 nameOfAction 的末尾添加斜杠也可以将 @Field 更改为 @Query:

    @Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@POST("nameOfAction/")
Call<ItemResponse> callAction(@Header("Authorization") String authorization, @Query("nameOfParameter") String nameOfParameter);

关于android - 发送参数和主体行空改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com