[英]Android how to get json array from retrofit
Here is the code of retrofit request这是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);
}
}
Here is the apiservice这是api服务
@POST("dev-wallet-spanner")
Call<Object> getTransactions(@Header("x-api-key") String key,
@Body TransactionDetails transactionDetails);
here the params which I am passing in postman这是我在 postman 中传递的参数
{
"op_type":"employee_wise_date",
"bid":"21",
"gid":"09",
"from_date":"2019-01-01",
"to_date":"2022-07-01"
}
Here is response which is coming in postman这是 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"
}
]
I am using retrofit to make api request in my android application.我正在使用 retrofit 在我的 android 应用程序中发出 api 请求。 I am passing parameters with retrofit but I am not getting response.But the same parameters working well with postman.In postman I am getting exact response when I try that with code I am not getting response.我正在使用 retrofit 传递参数,但我没有得到响应。但是相同的参数适用于 postman。在 postman 中,当我尝试使用代码我没有得到响应时,我得到了准确的响应。
I am passing the same values which I passed in postman but the response is not coming properly.我传递的值与在 postman 中传递的值相同,但响应不正确。 I am setting values in the modal class then I am passing that modal class in retrofit.But response is not coming.please help out with this to get values.我在模态 class 中设置值,然后我在 retrofit 中传递模态 class。但是响应没有到来。请帮助获取值。
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);
// and in Java file same change must be done Replace Object with JsonElement // 并且在 Java 文件中必须进行相同的更改 用 JsonElement 替换 Object
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);
}
}
in my case, i use ResponseBody
not Object
就我而言,我使用ResponseBody
而不是Object
So.所以。 try this.尝试这个。
1st, in API service, 1、在API服务中,
@FormUrlEncoded //add this
@POST("dev-wallet-spanner")
Call<ResponseBody> getTransactions(@Header("x-api-key") String key, @Body TransactionDetails transactionDetails);
2nd, change in java code. 2、更改java代码。
call1.enqueue(new Callback < ResponseBody> () {
@Override
public void onResponse(Call < ResponseBody> call,
Response < ResponseBody> response) {
try {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(response.body().string());
if (response.isSuccessful()) {
common.showtoast("succes", getApplicationContext());
//get data using try and catch
JSONArray jsonDataArray = jsonObject.getJSONArray("data");
//jsonDataArray is what List from retrofit
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.