2017-04-23 78 views
2

我在學習如何使用Volley。我已將響應成功記錄到System.out.printIn,並在logcat中查看數組中的單個項目。當我有多個項目時,我正在用FOR LOOP掙扎。我無法弄清楚如何循環數據。有人可以幫助請告訴我如何去做。Android Volley響應循環

我的MainActivity

package hfad.com.volley_listview_array; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
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.util.ArrayList; 
public class MainActivity extends Activity { 
    ArrayAdapter<String> adapter; 
    ArrayList<String> items; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     final TextView mTextView = (TextView) findViewById(R.id.textView); 

     String url = "https://reqres.in/api/users?page=2"; 

     JsonObjectRequest jsonRequest = new JsonObjectRequest 
       (Request.Method.GET, url, new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         // the response is already constructed as a JSONObject! 
         try { 
          response = response.getJSONObject("data"); 

          //for loop goes here? How? I get i already defined in scope 
          for (int i = 0, i < response.length(), i++); 
          //String site = response.getString("first_name"), 
           //  network = response.getString("last_name"); 
          //System.out.println("firstname: "+site+"\nLastname: "+network); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError error) { 
         error.printStackTrace(); 
        } 
       }); 

     Volley.newRequestQueue(this).add(jsonRequest); 


    }} 

而且我activity_main.xml中我並不需要在列表中顯示的結果。只需要將所有數據一個接一個堆起來。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="10dp" 
    tools:context=".MainActivity"> 


    <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 
    <!-- 
    <ListView 
     android:id="@+id/listv" 
     android:layout_width="match_parent" 
     android:layout_height="89dp"></ListView> 
    --> 

</LinearLayout> 

我的數據是從本次測試API https://reqres.in/api/users?page=2

{"page":"2","per_page":3,"total":12,"total_pages":4,"data":[{"id":4,"first_name":"eve","last_name":"holt","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"},{"id":5,"first_name":"gob","last_name":"bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"},{"id":6,"first_name":"tracey","last_name":"bluth","avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"}]} 

我得到的數據顯示在我的logcat的,但它只能顯示第一項。如何遍歷所有記錄。我感謝任何幫助。

+0

嘗試的JSONArray使用GSON POR傑克遜賽瑞亞/ deserializators –

回答

1
String url = "https://reqres.in/api/users?page=2"; 

     JsonObjectRequest jsonRequest = new JsonObjectRequest 
       (Request.Method.GET, url, new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         try { 
          JSONArray jsonArray = response.getJSONArray("data"); 
          JSONObject jsonInnerObject; 

          for (int k = 0; k< jsonArray.length(); k++){ 

           jsonInnerObject=new JSONObject(jsonArray.get(k).toString()); 

           String site = jsonInnerObject.getString("first_name"), 
            network = jsonInnerObject.getString("last_name"); 
           System.out.println("firstname: "+site+"\nLastname: "+network); 
          } 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError error) { 
         error.printStackTrace(); 
        } 
       }); 

     Volley.newRequestQueue(this).add(jsonRequest);` 

注:數據中包含的JSONObject

+0

非常感謝你爲這個明確的答案。我很感激。爲什麼我給我錯誤?我應該使用任何其他字母,但「我」? – Marco

+0

@Marco你可以使用「我」循環。我在我的android studio上測試它,「我」已經在我的課堂上使用,所以我用k –

+0

我明白了。謝謝你的幫助。 – Marco