2017-03-04 97 views
0

我想用tutorial (source code)解析這個json (my)如何使用Volley解析JSON?

我的代碼是:

public class MainActivity extends Activity { 
    // Log tag 
    private static final String TAG = MainActivity.class.getSimpleName(); 

    // Movies json url 
    private static final String url = "http://api.androidhive.info/json/movies.json"; 
    private ProgressDialog pDialog; 
    private List<Movie> movieList = new ArrayList<Movie>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     listView = (ListView) findViewById(R.id.list); 
     adapter = new CustomListAdapter(this, movieList); 
     listView.setAdapter(adapter); 

     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 

     // changing action bar color 
     getActionBar().setBackgroundDrawable(
     new ColorDrawable(Color.parseColor("#1b1b1b"))); 

     // Creating volley request obj 
     JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener <JSONArray>() { 
      @Override 
      public void onResponse(JSONArray response) { 
       Log.d(TAG, response.toString()); 
       hidePDialog(); 

       // Parsing json 
       for (int i = 0; i < response.length(); i++) { 
        try { 

         JSONObject obj = response.getJSONObject(i); 
         Movie movie = new Movie(); 
         movie.setTitle(obj.getString("title")); 
         movie.setThumbnailUrl(obj.getString("image")); 
         movie.setRating(((Number) obj.get("rating")).doubleValue()); 
         movie.setYear(obj.getInt("releaseYear")); 

         // Genre is json array 
         JSONArray genreArry = obj.getJSONArray("genre"); 
         ArrayList<String> genre = new ArrayList<String>(); 
         for (int j = 0; j < genreArry.length(); j++) { 
          genre.add((String) genreArry.get(j)); 
         } 
         movie.setGenre(genre); 

         // adding movie to movies array 
         movieList.add(movie); 

        } catch(JSONException e) { 
         e.printStackTrace(); 
        } 

       } 

       // notifying list adapter about data changes 
       // so that it renders the list view with updated data 
       adapter.notifyDataSetChanged(); 
      } 
     }, 
     new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        hidePDialog(); 

       } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(movieReq); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     //getMenuInflater().inflate(R.menu.main, menu); 
     int i = 1; 
     return true; 
    } 

} 

此代碼是從androidhive,但我不知道如何解析JSON(我的)。

那麼,如何解析json呢?

+1

[發送和解析JSON對象]的可能的複製(http://stackoverflow.com/questions/2818697/sending-and-parsing-json-objects) – UMESH0492

回答

1

試試這個:

String url = "http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC"; 
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      try { 
       JSONObject jsonObject = new JSONObject(response); 
       JSONArray jsonArray = jsonObject.getJSONArray("data"); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jo = jsonArray.getJSONObject(i); 
        // Do you fancy stuff 
        // Example: String gifUrl = jo.getString("url"); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      // Anything you want 
     } 
    }); 
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
    requestQueue.add(stringRequest); 
+0

可以爲您節省通過使用'JsonObjectRequest'而不是'StringRequest'的解析行;) 看一看[這裏](https://developer.android.com/training/volley/request.html) –

+0

是的我知道:D –

+0

它是沒有給出任何錯誤,但它也沒有顯示json.my代碼是: –