使用Volley的Android Contacts App的JSON解析问题

时间:2017-05-22 18:07:20

标签: java android json listview

我在解析此JSON文件时遇到问题。我收到了这个错误 05-22 03:47:03.337 10271-10271 / com.thesis.luna.contacts W / System.err:org.json.JSONException:

没有值

https://s3.amazonaws.com/technical-challenge/Contacts.json

是文件。

没有给出ArrayName,所以我把jsonString留空了#34; "

这是我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

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.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class MainActivity extends AppCompatActivity {
    // Will show the string "data" that holds the results
    TextView results;
    // URL of object to be parsed
    String JsonURL = "https://s3.amazonaws.com/technical-challenge/Contacts.json";
    // This string will hold the results
    String data = "";
    // Defining the Volley request queue that handles the URL request concurrently
    RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Creates the Volley request queue
        requestQueue = Volley.newRequestQueue(this);

        // Casts results into the TextView found within the main layout XML with id jsonData
        results = (TextView) findViewById(R.id.name);

        // Creating the JsonArrayRequest class called arrayreq, passing the required parameters
        //JsonURL is the URL to be fetched from
        JsonArrayRequest arrayreq = new JsonArrayRequest(JsonURL,
                // The second parameter Listener overrides the method onResponse() and passes
                //JSONArray as a parameter
                new Response.Listener<JSONArray>() {

                    // Takes the response from the JSON request
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            // Retrieves first JSON object in outer array
                            JSONObject colorObj = response.getJSONObject(0);
                            // Retrieves "colorArray" from the JSON object
                            JSONArray colorArry = colorObj.getJSONArray("");
                            // Iterates through the JSON Array getting objects and adding them
                            //to the list view until there are no more objects in colorArray
                            for (int i = 0; i < colorArry.length(); i++) {
                                //gets each JSON object within the JSON array
                                JSONObject jsonObject = colorArry.getJSONObject(i);


                                String name = jsonObject.getString("name");

                                // Adds strings from the current object to the data string
                                //spacing is included at the end to separate the results from
                                //one another
                                data += " Name: " + name;
                            }
                            // Adds the data string to the TextView "results"
                            results.setText(data);
                        }
                        // Try and catch are included to handle any errors due to JSON
                        catch (JSONException e) {
                            // If an error occurs, this prints the error to the log
                            e.printStackTrace();
                        }
                    }
                },
                // The final parameter overrides the method onErrorResponse() and passes VolleyError
                //as a parameter
                new Response.ErrorListener() {
                    @Override
                    // Handles errors that occur due to Volley
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Volley", "Error");
                    }
                }
        );
        // Adds the JSON array request "arrayreq" to the request queue
        requestQueue.add(arrayreq);
    }
}

1 个答案:

答案 0 :(得分:0)

更新onResponse()方法,如下所示:

   ............................
   ......................................

                @Override
                public void onResponse(JSONArray response) {
                    try {

                        for (int i = 0; i < response.length(); i++) {
                            //gets each JSON object within the JSON array
                            JSONObject jsonObject = response.getJSONObject(i);


                            String name = jsonObject.getString("name");
                            String company = jsonObject.getString("company");
                            String email = jsonObject.getString("email");
                            String website = jsonObject.getString("website");


                            // Phone
                            JSONObject jsonObjectPhone = jsonObject.getJSONObject("phone");

                            String work = jsonObjectPhone.getString("work"); 
                            String home = jsonObjectPhone.getString("home"); 
                            String mobile = jsonObjectPhone.getString("mobile"); 


                            // Address
                            JSONObject jsonObjectAddress = jsonObject.getJSONObject("address");

                            String street = jsonObjectAddress.getString("street"); 
                            String city = jsonObjectAddress.getString("city"); 
                            String state = jsonObjectPhone.getString("state"); 
                            String country = jsonObjectAddress.getString("country"); 
                            String zip = jsonObjectAddress.getString("zip");  
                            double latitude = jsonObjectAddress.getDouble("latitude"); 
                            double longitude = jsonObjectAddress.getDouble("longitude"); 

                            // Do something with this values
                            ..........
                            ........................

                            //one another
                            data += " Name: " + name;
                        }
                        // Adds the data string to the TextView "results"
                        results.setText(data);
                    }
                    // Try and catch are included to handle any errors due to JSON
                    catch (JSONException e) {
                        // If an error occurs, this prints the error to the log
                        e.printStackTrace();
                    }
                }

............................
........................................

希望这会有所帮助〜