2016-08-22 10 views
0

enter image description herejsonによる解析でデータが取得されない

私のコードでjsonデータを解析しています。 jsonデータは、画像に示されているとおりです。なぜ私はURLをヒットしながら取得していない。 アクセスに問題があると思います。その後、アレイ内のレストランはオブジェクト内でそれから項目にアクセスし、問題にアクセスします。

だから誰でも助けてくれますか?私はどこにこれが欠けているのですか?

public class ListViewActivity extends Activity { 
    // Log tag 
    private static final String TAG = ListViewActivity.class.getSimpleName(); 
    // change here url of server api 
    private static final String url = "https://comida-95724.herokuapp.com/api/v1/restaurants/get_featured_restaurants"; 
    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_listview); 
     listView = (ListView) findViewById(R.id.list); 
     adapter = new CustomListAdapter(this, movieList); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Movie movie = movieList.get(position); 
       Intent intent = new Intent(ListViewActivity.this, SecondActivity.class); 
       intent.putExtra("name", movie.getName()); 
       intent.putExtra("average_ratings", movie.getAverage_ratings()); 
       intent.putExtra("full_address", movie.getAddress()); 
       intent.putExtra("image_url", movie.getThumbnailUrl()); 
       intent.putExtra("cuisine",movie.getCuisine()); 
       intent.putExtra("cost",movie.getCost()); 
       startActivity(intent); 

      } 
     }); 
     listView.setAdapter(adapter); 
     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Please Keep patience.Its loading..."); 

     pDialog.show(); 

     JsonObjectRequest movieReq = new JsonObjectRequest(Request.Method.GET, 
     url, null, new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         Log.d(TAG, response.toString()); 
         hidePDialog(); 
         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 
           JSONArray restaurantsJSONArray=response.getJSONArray("restaurants"); 
           JSONObject obj = response.getJSONObject(i); 
           Movie movie = new Movie(); 
           //movie.setTitle(obj.getString("title")); 
           movie.setName(obj.getString("name")); 
           //movie.setThumbnailUrl(obj.getString("image")); 
           movie.setThumbnailUrl(obj.getString("org_image_url")); 
           movie.setAverage_ratings(obj.getString("average_ratings")); 
           movie.setCuisine(obj.getString("cuisine")); 
           movie.setAddress(obj.getJSONObject("address").getString("area")); 
           // movie.setAddress(obj.getJSONObject("address").getString("full_address")); 
           movie.setCost(obj.getString("cost")); 

           movieList.add(movie); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 


         adapter.notifyDataSetChanged(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 

      } 
     }); 


     AppController.getInstance().addToRequestQueue(movieReq); 
    } 

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

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



} 

答えて

1

これを試してみてください。

JsonObjectRequest movieReq = new JsonObjectRequest(Method.GET, 
     urlJsonObj, null, new Response.Listener<JSONObject>() 

アクセス用のjsonArrayを:

JSONArray restaurantsJSONArray=response.getJSONArray("restaurants"); 

はこれを試してみてください。

JsonObjectRequest movieReq = new JsonObjectRequest(Request.Method.GET, 
     url, null, new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         Log.d(TAG, response.toString()); 
         JSONArray 

      restaurantsJSONArray=response.getJSONArray("restaurants"); 

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

           JSONObject obj = response.getJSONObject(i); 
           Movie movie = new Movie(); 
           //movie.setTitle(obj.getString("title")); 
           movie.setName(obj.getString("name")); 
           //movie.setThumbnailUrl(obj.getString("image")); 
           movie.setThumbnailUrl(obj.getString("org_image_url")); 
           movie.setAverage_ratings(obj.getString("average_ratings")); 
           movie.setCuisine(obj.getString("cuisine")); 
           movie.setAddress(obj.getJSONObject("address").getString("area")); 
           // movie.setAddress(obj.getJSONObject("address").getString("full_address")); 
           movie.setCost(obj.getString("cost")); 

           movieList.add(movie); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 


         adapter.notifyDataSetChanged(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 

      } 
     }); 


     AppController.getInstance().addToRequestQueue(movieReq); 
    } 
+0

変更を加えてコードを編集しました – user6313669

+0

@ user6313669私の編集した回答を確認してください –

+0

JSONObject obj = response.getJSONObject(i);この行のエラー – user6313669

1

利用JsonObjectRequestの代わりJsonArrayRequestを理由ポストイメージとして代わりにJSONArrayのルート項目としてJSONObjectが含まれています。

およびrestaurantsは、JSONArrayであり、JSONObjectである。最初にそれからすべてのJSONObjectのを取得する前にonResponse JSONObjectパラメータのrestaurantsを得る:

JSONArray restaurantsJSONArray=response.getJSONArray("restaurants"); 

は今restaurantsJSONArrayからJSONObjectを取得するためにループを使用します。

+0

私は変更を加えて編集コード私は – user6313669

+1

user6313669 @得ていないのです参照してくださいしている。動き 'JSONArray restaurantsJSONArray = response.getJSONArray( "レストラン"); forループの外'ラインを使い、 'のために(int型私= 0; I

関連する問題