0

I am getting the response from the backend in the form of an array of objects and I stored the response in an array list and I want to retrieve the specific field value from the ArrayList and attach it to the listview.

This is my code :

Retrofit retrofit = builder.build();

        APIService apiservice = retrofit.create(APIService.class);
        Call<PostData> call = apiservice.transactiondetails(UID);
        call.enqueue(new Callback<PostData>() {
            @Override
            public void onResponse(@NonNull Call<PostData> call, @NonNull Response<PostData> response) {
                if (response.code() == 200) { // this  is the case for success
                    if (response.isSuccessful()) {
                        response.body();
//                        ArrayList dp = new HashMap<String, Array>();
//                        dp = Objects.requireNonNull(response.body()).getToken1();
                      ArrayList  dp  = Objects.requireNonNull(response.body()).getToken1();
                        Log.d(TAG, String.valueOf(dp));
                        System.out.println("hash map is" + dp);
                        Intent i = new Intent(deyaPay.this, TransactionDetails.class);
                        i.putExtra("Transactiondetails",dp);
                        startActivity(i);
                        Toast.makeText(deyaPay.this, "success" + dp, Toast.LENGTH_SHORT).show();
                        //Toast.makeText(getApplicationContext(), dp, Toast.LENGTH_LONG).show();
                    }
                }

and this is class for getting the response from backend:

public class PostData {

    @SerializedName("Token")
    @Expose
    private String token;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    @SerializedName("Details")
    @Expose
    private Map<String,Array> token1;

    public Map<String, Array> getToken1() {
        return token1;
    }

    public void setToken1(Map<String,Array> token1) {
        this.token1 = token1;
    }


}

So how to attach the response to listview and This is my response from backend :

[
        {
        Amount = 4;
        TransactionID = deyaPay;
        method = "Money Sent using deyaPay";
        timestamp = "2018-09-03T06:01:41.306Z";
    },
        {
        Amount = 2;
        Token = "tok_1D69olLMFuWYhKXrAqwmTprz";
        TransactionID = "ch_1D69omLMFuWYhKXrYpbNEtGH";
        method = "Requested by";
        timestamp = "2018-09-03T04:37:40.147Z";
    },
        {
        Amount = 5000;
        TransactionID = deyaPay;
        method = "Money Sent using deyaPay";
        timestamp = "2018-09-03T04:01:00.565Z";
    },
        {
        Amount = 50;
        TransactionID = deyaPay;
        method = "Money Sent using deyaPay";
        timestamp = "2018-09-03T03:59:01.708Z";
    },
        {
        Amount = 2;
        TransactionID = deyaPay;
        method = "Money Sent using deyaPay";
        timestamp = "2018-09-03T03:58:26.964Z";
    },
        {
        Amount = 1;
        Token = "tok_1D68nBLMFuWYhKXrGM2AN2IF";
        method = "Add Money to deyaPay account using credit card/debit card";
        timestamp = "2018-09-03T03:31:58.008Z";
    },
        {
        Amount = 5;
        Token = "tok_1D68cpLMFuWYhKXrM7oc2X7j";
        method = "Add Money to deyaPay account using credit card/debit card";
        timestamp = "2018-09-03T03:21:16.281Z";
    },
        {
        Amount = 2;
        Token = "tok_1D5pE1LMFuWYhKXr9BMvQ1dm";
        method = "Add Money to deyaPay account using credit card/debit card";
        timestamp = "2018-09-02T06:38:22.188Z";
    },
        {
        Amount = 2;
        Token = "tok_1D5pCxLMFuWYhKXrJoRcbN8o";
        method = "Add Money to deyaPay account using credit card/debit card";
        timestamp = "2018-09-02T06:37:16.393Z";
    }]
vijju
  • 462
  • 9
  • 30
  • It is not valid JSON. – Sumesh TG Sep 03 '18 at 11:21
  • It is a valid json . How do you say it is not valid json? It is worked in IOS to attach these response in table View.But in android – vijju Sep 03 '18 at 11:25
  • `[{ "Amount": 4, "TransactionID": "deyaPay", "method": "Money Sent using deyaPay", "timestamp": "2018-09-03T06:01:41.306Z" }]` This is valid and parsable. Your reponse have `;` and `=` so couldn't parse it to an object. – Sumesh TG Sep 03 '18 at 11:26
  • @vijju Just copied urs response and pasted it on it on http://json.parser.online.fr/ . ...It cleraly shows it is invalid..Please check it once – Ravindra Kushwaha Sep 03 '18 at 11:27
  • If it is working in IOS they won't treat it as `JSON` may be something else. – Sumesh TG Sep 03 '18 at 11:29
  • Sumesh TG okk. How to retrive the speciflc field values from that format and attach to list view – vijju Sep 03 '18 at 11:46
  • Unrelated: read https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – GhostCat Sep 03 '18 at 19:02

1 Answers1

0

Your json should look like that:

[{
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    },
    {
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    },
    {
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    },
    {
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    },
    {
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    },
    {
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    },
    {
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    },
    {
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    },
    {
        "Amount": "4",
        "Token": "tok_1D69olLMFuWYhKXrAqwmTprz",
        "TransactionID": "deyaPay",
        "method": "Money Sent using deyaPay",
        "timestamp": "2018-09-03T06:01:41.306Z"
    }
]

And your response should be an array of objects. That is the error that you are getting, you are trying to parse an object when the response is an array.

In your listview adapter you set the text to the item with the values from the arraylist:

amount.setText(data.get(position).getAmount());
       token.setText(data.get(position).getToken());

I let you here an example about how to implement a listview with custom adapter that is what you need: https://medium.com/mindorks/custom-array-adapters-made-easy-b6c4930560dd

Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8
  • When i used the array list it shows the success response. But that response i stored it and i want to attach it to list view. I thought it is in key value pair . so i instead of array list i tried to use the hash map. so that why it shows that error. – vijju Sep 03 '18 at 11:35
  • First you need parse the response in the array list and then with those items saved in the arraylist you just need pass the array list to your listview and work with the arraylist, you dont need a hashmap to attach the the data in the listview – Juanjo Berenguer Sep 03 '18 at 11:39
  • So when it is array of objects then how to retrive the the Amount and Token corresponded values into list view – vijju Sep 03 '18 at 11:40
  • First, I took it into an Arraylist .ArrayList dp = Objects.requireNonNull(response.body()).getToken1(); Here dp stored all the response came from the backend. So i want to retrive the specifie field values from array list and attach it to list view – vijju Sep 03 '18 at 11:44