aligassan
aligassan

Reputation: 23

volley json array get only last element

package net.myaapp.app.weatherapp;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class newsfeed extends AppCompatActivity {
    RequestQueue rq;

    TextView message_title;
    private String message ;
    String url = "https://graph.facebook.com/v2.10/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newsfeed);
        rq = Volley.newRequestQueue(this);

        message_title = (TextView)findViewById(R.id.message_feed) ;

        sendjsonrequest();

    }

    public void sendjsonrequest() {

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONObject feed = response.getJSONObject("feed");
                    JSONArray jsonArray = feed.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject data = jsonArray.getJSONObject(i);

                        message = data.getString("message");

                        message_title.setText(message);
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        rq.add(jsonObjectRequest);

    }



    private String getDate(long timeStamp) {
        try {
            DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            Date netDate = (new Date(timeStamp));
            return sdf.format(netDate);
        } catch (Exception ex) {
            return "xx";
        }
    }
}

I am actually try to get news feed from my facebook page , and the code working fine but unfortunately doesn't show all array list , i get last element only , data json content array [10] . i get only element number 9 and the other element not show . maybe the problem withe array list ? Any ideas? Am I missing something?

Upvotes: 1

Views: 782

Answers (1)

p.mathew13
p.mathew13

Reputation: 921

I believe this is happening because you are overwriting your text view content with a new message each time the JSON array is looped through.So if you want to display all messages you better append the new messages to the previously looped messages and print it outside the loop.

            for (int i=0;i<jsonArray.length();i++){
                    JSONObject data = jsonArray.getJSONObject(i);

                    message += data.getString("message")+"\n";     
                }
           message_title.setText(message);

Upvotes: 2

Related Questions