2017-02-01 4 views
2

Я разборе значение JSON в GridView, но почему-то его не показывает никакой ценности в GridView, я путаюсь в JSon коде, как я думаю, что я что-то не хватает в этом code..kindly проверить:Это правильный способ разбора json?

private void getData() { 
     //Showing a progress dialog while our app fetches the data from url 
     final ProgressDialog loading = ProgressDialog.show(this, "Please wait...", "Fetching data...", false, false); 
     String DATA_URL = "http://........nList"; 

     StringRequest stringRequest = new StringRequest(Request.Method.POST, DATA_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         //Toast.makeText(PMPigeonListingActivity.this,response,Toast.LENGTH_LONG).show(); 
         loading.dismiss(); 
         try { 
          JSONArray json = new JSONArray(response); 
          for (int i = 0; i < json.length(); i++) { 
           //Creating a json object of the current index 
           JSONObject obj = null; 
           try { 
            //getting json object from current index 
            obj = json.getJSONObject(i); 
            //getting image url and title from json object 
            pid.add(obj.getInt(String.valueOf(TAG_PID))); 
            pname.add(obj.getString(TAG_PNAME)); 
            pdetails.add(obj.getString(TAG_PDETAILS)); 
            pmobile.add(obj.getString(TAG_MOBILE)); 
            pemail.add(obj.getString(TAG_EMAIL)); 
            images.add(obj.getString(TAG_IMAGE_URL)); 
            names.add(obj.getString(TAG_NAME)); 
           } catch (JSONException e) { 
            e.printStackTrace(); 
           } 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

         //Creating GridViewAdapter Object 
         PMPigeonListAdapter pmpigeonlistadapter = new PMPigeonListAdapter(getApplicationContext(), images, names, pid, pdetails, pmobile, pemail, pname); 

         //Adding adapter to gridview 
         pmpigeonlistadapter.notifyDataSetChanged(); 
         gridView.setAdapter(pmpigeonlistadapter); 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         //Toast.makeText(PMPigeonListingActivity.this, error.toString(), Toast.LENGTH_LONG).show(); 
        } 
       }) { 
      @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("country", PostCountry); 
       params.put("strain", PostStrain); 
       params.put("distance", PostDistance); 
       return params; 
      } 

     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 


    } 

это мой JSON выход:

{ 
    "status_code": 200, 
    "status": "OK", 
    "status_message": "Success", 
    "pigeon_list": [ 
    { 
     "id": "1", 
     "pigeon_name": "sofiee", 
     "auth_token": "58809c7129a5a", 
     "country_code": "AE", 
     "strain_id": "75", 
     "distance": "3", 
     "pigeon_price": "50.00", 
     "pigeon_details": "One of the best ", 
     "image": "http:.98a8ac5.jpeg", 
     "pedigree_image": "http://...1.jpeg",... 
     "status": "", 
     "created": "2017-01-19 16:52:14", 
     "updated": "0000-00-00 00:00:00", 
     "strain_name": "Janssen/gaston wowers ", 
     "usr_mobile": "+971/505040009", 
     "usr_image": "http://....19a.jpeg", 
     "usr_email": "[email protected]" 
    }, 

Я получаю ответ в тосте также, только проблема в том, JSON Тре ...

это код PHP:

public function searchPigeonList() 
    { 
     $data = (array)$this->request->input('json_decode'); 

     $returnArr = $this->resp_arr; 
     $returnArr['pigeon_list'] = array(); 
     $conn = ConnectionManager::get('default'); 

     $query = "SELECT `pg`.*,`ps`.name as strain_name,`us`.mobile as usr_mobile,`us`.image as usr_image,`us`.email as usr_email FROM 
`pigeons` as `pg` INNER JOIN `users` as `us` ON `pg`.`auth_token` = `us`.`uniq_id` INNER JOIN `pigeon_strain` as `ps` ON `ps`.`id` = `pg`.`strain_id` "; 

//   $query .= "WHERE `pg`.`country_code` = '".$data['country_code']."' "; 
     $cnt_cd = $data['country_code']; 
     $str_id = $data['strain_id']; 
     $dst = $data['distance']; 

     $conditions = array(); 
     if($cnt_cd !="") { 
      $conditions[] = "`pg`.country_code='$cnt_cd'"; 
     } 
     if($str_id !="") { 
      $conditions[] = "`pg`.strain_id='$str_id'"; 
     } 
     if($dst !="") { 
      $conditions[] = "`pg`.distance='$dst'"; 
     } 
     if (count($conditions) > 0) { 
      $query .= " WHERE " . implode(' AND ', $conditions); 
      $query .= " AND `pg`.status='approved'"; 
     } 
//echo $query;exit; 
     $stmt = $conn->execute($query); 
     $returnArr['status_code'] = 200; 
     $returnArr['status'] = "OK"; 
     $returnArr['status_message'] = "Success"; 
     $returnArr['pigeon_list'] = $stmt ->fetchAll('assoc'); 
     if ($this->request->is('post')) { 
      echo json_encode($returnArr); 
      exit; 
     } 

    } 
+0

ли вы видите какие-либо ошибки/исключения в журналах? – Raghunandan

+0

Вы получаете JSONArray в ответ? – Prashant

+0

не исключение n ошибка, ее просто не показано anuthin в gridview –

ответ

1
try { 
    JSONArray json = new JSONObject(response).getJSONArray("pigeon_list"); 
    for (int i = 0; i < json.length(); i++) { 
     JSONObject obj = null; 
     try { 
      obj = json.getJSONObject(i); 
      pid.add(obj.getInt("id")); 
      pname.add(obj.getString("pigeon_name")); 
      pdetails.add(obj.getString("pigeon_details")); 
      pmobile.add(obj.getString("usr_mobile")); 
      pemail.add(obj.getString("usr_email")); 
      images.add(obj.getString("usr_image")); 
      names.add(obj.getString("pigeon_name")); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
}catch(JSONException je){ 
    je.printStackTrace(); 
}catch(Exception e){ 
    e.printStackTrace(); 
} 

попробуйте заменить

+0

thx, его workin, –

0

Попробуйте это.

public void onResponse(String response) { 
        //Toast.makeText(PMPigeonListingActivity.this,response,Toast.LENGTH_LONG).show(); 
        loading.dismiss(); 
        try { 
         JSONObject responseObject=new JSONObject(response); 
         JSONArray json = responseObject.getJSONArray("pigeon_list"); 
         for (int i = 0; i < json.length(); i++) { 
          //Creating a json object of the current index 
          JSONObject obj = null; 
          try { 
           //getting json object from current index 
           obj = json.getJSONObject(i); 
           //getting image url and title from json object 
           pid.add(obj.getInt(String.valueOf(TAG_PID))); 
           pname.add(obj.getString(TAG_PNAME)); 
           pdetails.add(obj.getString(TAG_PDETAILS)); 
           pmobile.add(obj.getString(TAG_MOBILE)); 
           pemail.add(obj.getString(TAG_EMAIL)); 
           images.add(obj.getString(TAG_IMAGE_URL)); 
           names.add(obj.getString(TAG_NAME)); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        //Creating GridViewAdapter Object 
        PMPigeonListAdapter pmpigeonlistadapter = new PMPigeonListAdapter(getApplicationContext(), images, names, pid, pdetails, pmobile, pemail, pname); 

        //Adding adapter to gridview 
        pmpigeonlistadapter.notifyDataSetChanged(); 
        gridView.setAdapter(pmpigeonlistadapter); 

       } 
+0

Я ничего не получаю в griview –

+0

Опубликовать вывод logcat –

+0

@rahul, что вы изменили, за исключением преобразования одной строки в другую, чтобы сделать JSONArray? – Prashant