Nested for Loop index is not in order

Viewed 28

MainActivity.java

public class MainActivity extends AppCompatActivity {

String server_ip = "192.168.252.100";
    String url = "http://"+server_ip+"/lib/androidAPI.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    String[] list = {"Action","Adventure", "Family"};
            for(int x=0;x<list.length;x++){
            int y = x;

            movie_class test = new movie_class();
            test.getJsonResponse("Get__Movies_By_Genre_Limit", list[x], getApplicationContext(), new VolleyCallback() {
                @Override
                public void onSuccess(JSONArray result2) {
                    try {
                        System.out.println("array list using y as index: ("+y+")="+list[y]);
                        System.out.println("-Database Query Results");
                        for (int j = 0; j < result2.length(); j++) {
                            JSONObject obj = result2.getJSONObject(j);
                            System.out.println("--(" + j + ")" + obj.getString("name"));
                        }
                    }catch (JSONException e){

                    }
                }
                @Override


                   public void onError(String result) {
                        System.out.println("(error) populate genres " + result);
                    }
                });
            }
        }
        public interface VolleyCallback {
            void onSuccess(JSONArray result);
            void onError(String result);
        }
    }

    

movie_class.java

public class movie_class extends MainActivity{
public void getJsonResponse(String getType, String parameters, Context mainContext, final VolleyCallback callback) {
    StringRequest strReq = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        //JSONArray array = new JSONArray();
                        JSONArray array = jsonObject.getJSONArray("array");
                        callback.onSuccess(array);
                    }catch (JSONException e){

                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            callback.onError(volleyError + "");
        }
    }){
        @Override
        public String getBodyContentType() {
            // as we are passing data in the form of url encoded
            // so we are passing the content type below
            return "application/x-www-form-urlencoded; charset=UTF-8";
        }

        @Override
        protected Map<String, String> getParams() {

            // below line we are creating a map for storing our values in key and value pair.
            Map<String, String> params = new HashMap<String, String>();

            // on below line we are passing our key and value pair to our parameters.
            params.put("getType", getType);
            params.put("parameters", parameters);

            // at last we are returning our params.
            return params;
        }
    };
    Volley.newRequestQueue(mainContext).add(strReq);
}

}

Output 1:

array list using y as index: (0)=Action
-Database Query Results
--(0)Air Force One
--(1)Ant-Man
array list using y as index: (2)=Family
-Database Query Results
--(0)Cocomelon
--(1)Finding Dory
array list using y as index: (1)=Adventure
-Database Query Results
--(0)Air Force One
--(1)Ant-Man

Output 2 without changing the codes:

array list using y as index: (1)=Adventure 
-Database Query Results 
--(0)Air Force One 
--(1)Ant-Man
array list using y as index: (0)=Action
-Database Query Results
--(0)Air Force One
--(1)Ant-Man
array list using y as index: (2)=Family
-Database Query Results
--(0)Cocomelon
--(1)Finding Dory

Question is why the for loop index (x) is not in order or randomly change order when using it inside the movie_class.getJsonResponse?

I'm using that technique many times in php but it was different in java.

1 Answers

I've solve the problem by manipulating the data in php file so that I will just call one request in client side android. :)

Related