2017-05-22 18 views
-1

このJSONファイルの解析中に問題が発生しました。 47::System.errの/ W 03.337 10271から10271/com.thesis.luna.contacts:org.json.JSONException:Android連絡先のJSON解析に問題があります。

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

ある値がありません私はこのエラー 5月22日03を取得していますファイル。

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

答えて

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

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

希望この私がここで

が私のコードである "" 空の私のjsonStringを残したので、それは何のArrayNameを与えられなかった

助けてくれる〜

関連する問題