简体   繁体   中英

Using Volley to Parse an array from PHP into Android

I am unable to find any resources about parsing an array into my android application from PHP. I am using Volley to handle the connection.

At the moment I have an array in the PHP script on the web server containing connections to a database which puts the data from MySQL query into the array.

$latitude = array (
    //Code for array here
);

$longitude = array (
    //Code for array here
);

I am able to connect to the script okay but cannot get figure out how to get each element from the array in PHP to an array in my mainActivity.java file.

The Java code is throwing an exception saying 'No value for Latitude' The way I understand this is because 'latitude' also tried '$latitude' is not the name of the array it is the variable name. In PHP I don't think there is such a name.

Is there another way to parse the contents of this array to Java. The Java code I have tried is:

String url = "urlHere";
    JSONObject jObj = new JSONObject();
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, jObj,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try
                    {
                        JSONArray jsonArray = response.getJSONArray("$latitude");
                        //Loop through array
                        for (int i = 0; i<jsonArray.length(); i++)
                        {
                            JSONObject locationItem = jsonArray.getJSONObject(i);
                            String latitude = locationItem.getString("index1");
                        }
                    }
                    catch (Exception e)
                    {
                        //Error encountered.
                        //Log Error
                        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            //Toast.makeText(getApplicationContext(), volleyError.toString(), Toast.LENGTH_LONG).show();
        }
    });

So the reason your getting an error is because

"latitude":
{
     "index1": "100", 
     "index2": "200",
}

should be ( in order to get your code to work ):

"latitude": [
         { "index1": "100" },
         { "index2": "200" },
]
  1. In the original JSON, you've just got a JSON object with keys that are named index1 and index2 .
  2. In the second JSON, you've got an array of JSON objects with index1 as a key in the first element and index2 in the second element.

UPDATE:
Even though the above will get your code to work . I recommend that you change the JSON to:

"latitude": [
    "100",
    "200",
]

and for your onResponse(...) method write:

@Override
public void onResponse(JSONObject response) {
    try
    {
        JSONArray jsonArray = response.getJSONArray("latitude");
        //Loop through array
        for (int i = 0; i<jsonArray.length(); i++)
        {
            String latitude = jsonArray.getString(i);
        }
    }
    // Rest of your code can stay the same
}

Seems more intuitive to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM