简体   繁体   English

使用Volley将数组从PHP解析到Android

[英]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. 我找不到有关从PHP将数组解析为我的android应用程序的任何资源。 I am using Volley to handle the connection. 我正在使用Volley处理连接。

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. 目前,我在Web服务器上的PHP脚本中有一个数组,其中包含一个与数据库的连接,该数据库将来自MySQL查询的数据放入该数组中。

$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. 我能够连接到脚本,但无法弄清楚如何将PHP中的数组中的每个元素都获取到mainActivity.java文件中的数组中。

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. Java代码引发了一个异常,指出“纬度无值”。据我所知,这是因为“纬度”也尝试了“ $ latitude”,而不是数组名,而是变量名。 In PHP I don't think there is such a name. 在PHP中,我认为没有这样的名字。

Is there another way to parse the contents of this array to Java. 还有另一种方法可以将该数组的内容解析为Java。 The Java code I have tried is: 我尝试过的Java代码是:

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 . 在原始JSON中,您刚刚得到了一个带有名为index1index2键的JSON对象。
  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. 在第二个JSON中,您具有一个JSON对象数组,其中index1作为键在第一个元素中,而index2在第二个元素中。

UPDATE: 更新:
Even though the above will get your code to work . 即使以上内容可以使您的代码正常工作 I recommend that you change the JSON to: 我建议您将JSON更改为:

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

and for your onResponse(...) method write: 并为您的onResponse(...)方法编写:

@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. 对我来说似乎更直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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