java - volley json 数组只获取最后一个元素

标签 java android json facebook

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";
        }
    }
}

我实际上是在尝试从我的 Facebook 页面获取新闻提要,代码工作正常但不幸的是没有显示所有数组列表,我只获取最后一个元素,数据 json 内容数组 [10]。我只得到元素号 9 而另一个元素没有显示。也许是数组列表的问题?有任何想法吗?我错过了什么吗?

最佳答案

我相信发生这种情况是因为每次循环 JSON 数组时,您都会用新消息覆盖 TextView 内容。因此,如果您想显示所有消息,最好将新消息附加到先前循环的消息中并打印它在循环之外。

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

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

关于java - volley json 数组只获取最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45499452/

相关文章:

java - Spring - applicationContext.xml 中的路径

java - Tomcat 不读取链接作为参数

java - Gson 使用空字段反序列化

json - 找不到可接受的代表

java - 我们如何使用 jpa hibernate 创建两个独立的表

java - 替换 GridView 中的项目

android - 方向更改后 fragment 中的 getActivity() 为空

javascript - 使用 $.getJSON 时可以将变量发送到 PHP 代码吗?

PHP:为 JSON 对象数组命名?

java - keytool - 查看公钥和私钥