Java Android如何从改造中获得json数组
这是改装请求的代码Java Android如何从改造中获得json数组,java,android,Java,Android,这是改装请求的代码 public void getTransactions() { if (common.isNetworkAvailable(WalletActivity.this)) { TransactionDetails transactionDetails = new TransactionDetails("employee_wise_date", bid, gid, "2019-01-01", "2022-07-01");
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);
}
}
这是邮递员的回复
{
"op_type":"employee_wise_date",
"bid":"21",
"gid":"09",
"from_date":"2019-01-01",
"to_date":"2022-07-01"
}
[
{
"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"
}
]
我正在使用改型在我的android应用程序中发出api请求。我正在通过改造传递参数,但没有得到响应。但是相同的参数在postman中运行良好。在postman中,当我尝试使用代码时,我得到了准确的响应,但没有得到响应
我传递的值与我在postman中传递的值相同,但响应不正确。我在模态类中设置值,然后在改装中传递该模态类。但是没有响应。请帮助获取值。在我的情况下,我使用
响应库
而不是对象
所以。试试这个
第一,在API服务中
@FormUrlEncoded //add this
@POST("dev-wallet-spanner")
Call<ResponseBody> getTransactions(@Header("x-api-key") String key, @Body TransactionDetails transactionDetails);
@FormUrlEncoded//添加此
@POST(“开发钱包扳手”)
调用getTransactions(@Header(“x-api-key”)字符串键,@Body TransactionDetails TransactionDetails);
第二,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
...
call1.enqueue(新回调(){
@凌驾
公共void onResponse(调用Call,
响应响应){
试一试{
JSONObject JSONObject=null;
试一试{
jsonObject=newJSONObject(response.body().string());
if(response.issusccessful()){
common.showtoos(“succes”,getApplicationContext());
//使用try-and-catch获取数据
JSONArray jsonDataArray=jsonObject.getJSONArray(“数据”);
//jsonDataArray是改造后的列表
...
add@FormUrlEncoded您可以使用JsonElement代替对象
呼叫
@FormUrlEncoded
@POST(“开发钱包扳手”)
调用getTransactions(@Header(“x-api-key”)字符串键,
@主体交易详情(交易详情);
//在Java文件中,必须进行相同的更改,用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);
}
}
if(common.isNetworkAvailable(WalletActivity.this)){
TransactionDetails TransactionDetails=新的TransactionDetails(“员工日期”,
标书,gid,“2019-01-01”、“2022-07-01”);
APIService=apiclient.getTransactions().create(APIService.class);
调用call1=service。
获取交易(“vqVZ1rEKGs1yKk5ZjOZj9Yco6HEZgoy6ArD9NCwf”,交易详情);
call1.enqueue(新回调(){
@凌驾
公共void onResponse(调用,
响应(响应){
if(response.issusccessful()){
common.showtoos(“succes”,getApplicationContext());
}
}
@凌驾
public void onFailure(调用Call,可丢弃的t){
common.showtoos(“获取事务失败”,getApplicationContext());
}
});
}否则{
common.showtoos(“无法连接到internet”,WalletActivity.this);
}
}
我尝试了你的建议,但仍然没有得到回应。当你使用改型时,你可以在logcat中检查参数(或标题)。这与postman相同吗?好的。然后检查帖子url,我编辑我的答案。在你的api服务中添加@FormUrlEncoded
。我已经通过使用modal类的arraylist解决了这个问题。谢谢你的回复
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);
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);
}
}