android - 发送参数和正文行空改造
问题描述
我正在尝试使用 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 空的工作。
如果我用 Postman 发送它,如下图所示:
任何建议将不胜感激。
解决方案
尝试像这样删除@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);
推荐阅读
- assembly - 哪些架构具有模数/非模数移位?
- c - 未找到 string.h 文件
- python - 在 Django 的 excel 文件中排列 JSON 数据
- c - 我对我的 if 条件有一些疑问
- react-native - 如何在登录功能中使用 navigation.navigate("Login")
- java - 任何在 gradle 文件中制作 `exclude` 的方法在构建时都不适用,但在运行代码时适用
- node.js - 你好。我正在尝试安装 sass loader 和 node sass 我试试这个 Yarn add Sass-loader@6.0.6 node-sass@4.5.3 但它不工作谢谢
- kubernetes - 为什么我在 ingress-nginx 上出现 502 bad gateway?
- javascript - Stardust.js 中的 D3.js 和 WebGL 交集
- android - 如何在 Android Studio 中使用多个关键字进行过滤?