2017-08-04 13 views
0
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のページからコードを取得しようとしていますが、コードは正常に動作していますが、残念ながらすべての配列リストを表示しません。アレイ[10]。私は要素番号9だけを取得し、他の要素は表示されません。おそらく配列リストに問題がありますか?何か案は?何か不足していますか?volley json array最後の要素だけを取得

+0

Volley's JsonObjectRequestにはいくつかの問題があります...代わりにStringRequestを使用してください – bharath

答えて

1

JSON配列がループスルーするたびにテキストビューのコンテンツを新しいメッセージで上書きするので、これが起こっていると思います。すべてのメッセージを表示したい場合は、以前にループしたメッセージに新しいメッセージを追加し、ループの外側に印刷します。

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

        message += data.getString("message")+"\n";  
       } 
      message_title.setText(message); 
関連する問題