gpt4 book ai didi

java - 如何从Retrofit获取json数组

转载 作者:行者123 更新时间:2023-12-01 16:14:58 24 4
gpt4 key购买 nike

这是Retrofit请求的代码

 public void getTransactions() {
if (common.isNetworkAvailable(WalletActivity.this)) {
TransactionDetails transactionDetails = new TransactionDetails("employee_wise_date",
bid, gid, "2019-01-01", "2022-07-01");

APIService service = apiclient.getTransactions().create(APIService.class);
Call < Object > call1 = service.
getTransactions("vqVZ1rEKGs1yKk5ZjOZj9Yco6HEZgoy6ArD9NCwf", transactionDetails);
call1.enqueue(new Callback < Object > () {
@Override
public void onResponse(Call < Object > call,
Response < Object > response) {

if (response.isSuccessful()) {
common.showtoast("succes", getApplicationContext());
}
}

@Override
public void onFailure(Call < Object > call, Throwable t) {
common.showtoast("Failed to get transaction", getApplicationContext());
}
});
} else {
common.showtoast("Unable to connect to the internet.", WalletActivity.this);
}
}

这是 api 服务

 @POST("dev-wallet-spanner")
Call<Object> getTransactions(@Header("x-api-key") String key,
@Body TransactionDetails transactionDetails);

这是我在 Postman 中传递的参数

{
"op_type":"employee_wise_date",
"bid":"21",
"gid":"09",
"from_date":"2019-01-01",
"to_date":"2022-07-01"
}

这是 postman 收到的回复

[
{
"transaction_id": 10166886,
"api_key": null,
"arn": "null",
"bid": "10653721",
"created_by": "118467809",
"created_on": "2018-06-03T13:12:46.886000000Z",
"gid": "11",
"ip": "2",
"reference_id": "model",
"remarks": "On Time Check-In",
"status": 1,
"transaction_amount": 100,
"transaction_type": "credit",
"currency": "INR",
"platform": "co",
"category": "f",
"transaction_date": "2018-04-01T01:01:01.683000000Z"
}
]

我正在使用 Retrofit 在我的 Android 应用程序中发出 api 请求。我正在使用 Retrofit 传递参数,但没有得到响应。但相同的参数在 Postman 中运行良好。在 postman 中,当我尝试使用代码时,我得到了准确的响应,但我没有得到响应。

我传递的值与在 Postman 中传递的值相同,但响应未正确进行。我在模态类中设置值,然后在 Retrofit 中传递该模态类。但没有回应。

最佳答案

 add @FormUrlEncoded you can use JsonElement in place of Object
Call<JsonElement>.......

@FormUrlEncoded
@POST("dev-wallet-spanner")
Call<JsonElement> getTransactions(@Header("x-api-key") String key,
@Body TransactionDetails transactionDetails);

//在 Java 文件中必须进行相同的更改,将 Object 替换为 JsonElement

    if (common.isNetworkAvailable(WalletActivity.this)) {
TransactionDetails transactionDetails = new TransactionDetails("employee_wise_date",
bid, gid, "2019-01-01", "2022-07-01");

APIService service = apiclient.getTransactions().create(APIService.class);
Call < JsonElement > call1 = service.
getTransactions("vqVZ1rEKGs1yKk5ZjOZj9Yco6HEZgoy6ArD9NCwf", transactionDetails);
call1.enqueue(new Callback < JsonElement > () {
@Override
public void onResponse(Call < JsonElement > call,
Response < JsonElement > response) {

if (response.isSuccessful()) {
common.showtoast("succes", getApplicationContext());
}
}

@Override
public void onFailure(Call < JsonElement > call, Throwable t) {
common.showtoast("Failed to get transaction", getApplicationContext());
}
});
} else {
common.showtoast("Unable to connect to the internet.", WalletActivity.this);
}
}

关于java - 如何从Retrofit获取json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62424972/

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