【问题标题】:JSON parsing trouble for Android Contacts App using Volley使用 Volley 的 Android 联系人应用程序的 JSON 解析问题
【发布时间】:2017-05-22 18:07:20
【问题描述】:

我无法解析这个 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 留空 " "

这是我的代码:

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);
    }
}

【问题讨论】:

    标签: java android json listview


    【解决方案1】:

    更新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();
                        }
                    }
    
    ............................
    ........................................
    

    希望对你有帮助~

    【讨论】:

      猜你喜欢
      • 2019-08-25
      • 2019-08-26
      • 1970-01-01
      • 2021-04-01
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      相关资源
      最近更新 更多