I'm using volley jar
to create customListView
below is the code i have used to implement . I have get this from open.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_inventory, container, false);
button=(Button)rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayView(7);
//launchQRScanner(view);
}
});
listView = (ListView) rootView.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), inventoryList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
Log.d("Url: ",url);
// Creating volley request obj
JsonArrayRequest inventoryReq = new JsonArrayRequest(url,new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
try {
JSONObject obj1 = response.getJSONObject(1);
int status = obj1.getInt("status");
if(status==1) {
JSONArray data=obj1.getJSONArray("data");
// Parsing json
for (int i = 0; i < data.length(); i++) {
JSONObject obj = data.getJSONObject(i);
Inventory inventory = new Inventory();
inventory.setNickName(obj.getString("nickname"));
inventory.setThumbUrl(obj.getString("thumb"));
inventory.setStockNumber(obj.getString("apron_id"));
inventory.setDOP(obj.getString("Date_purchase"));
inventory.setColor(obj.getString("color"));
// adding movie to movies array
inventoryList.add(inventory);
}
}
} catch (JSONException e) {
e.printStackTrace();
Log.d("JSON","JSON ERROR"+e.getMessage());
}
// 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());
Log.d("JSON ERROR",error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(inventoryReq);
return rootView;
}
Logcat
04-28 11:31:10.819 18300-18498/info.androidhive.slidingmenu D/dalvikvm﹕ GC_FOR_ALLOC freed 413K, 13% free 8257K/9479K, paused 19ms, total 20ms
04-28 11:31:10.827 18300-18300/info.androidhive.slidingmenu D/Volley﹕ [1] 3.onErrorResponse: MainActivity
04-28 11:31:10.827 18300-18300/info.androidhive.slidingmenu D/JSON ERROR﹕ org.json.JSONException: Value {"data":[{"apron_id":"123456","created_user":"2","Expiration":"2015-04-23","location":"13","nickname":"uyi","UID_no":"ikujki","Date_purchase":"2015-04-10","Batch":"ikk","update_time":"2015-04-10 05:22:38","color":"juki","manufacture":"0","garment_type":"yuyh","QTY":"898","core_material":"ytyt","created_time":"2015-04-10 05:22:38","apron_status":"0","inventory_id":"33","thumb":"http:\/\/demo.com\/test\/facility\/images\/no_photo.jpg","serial":"iui","apron_retire":"0"},{"apron_id":"12345mn","created_user":"2","Expiration":"2015-03-17","location":"12","nickname":"gfhgh","UID_no":"rtryttttttttt","Date_purchase":"2015-04-28","Batch":"rtrrtyy","update_time":"2015-04-10 05:15:54","color":"ytgtfghtg","manufacture":"0","garment_type":"hgjyhj","QTY":"7688","core_material":"fhgfhy","created_time":"2015-04-10 05:15:54","apron_status":"0","inventory_id":"32","thumb":"http:\/\/demo.com\/test\/facility\/images\/no_photo.jpg","serial":"hfh","apron_retire":"0"},{"apron_id":"12345df","created_user":"2","Expiration":"2015-05-15","location":"12","nickname":"rytyutu","UID_no":"rr546","Date_purchase":"2015-02-11","Batch":"rgf5676","update_time":"2015-04-10 04:50:45","color":"grey23","manufacture":"0","garment_type":"smooth","QTY":"45","core_material":"core2","created_time":"2015-04-10 04:50:45","apron_status":"0","inventory_id":"28","thumb":"http:\/\/demo.com\/test\/facility\/images\/no_photo.jpg","serial":"fgtg6","apron_retire":"0"},{"apron_id":"12345gh","created_user":"2","Expiration":"2015-03-12","location":"6","nickname":"hjytreeeeet","UID_no":"4566","Date_purchase":"2015-04-28","Batch":"gf545646","update_time":"2015-04-10 04:52:28","color":"green1","manufacture":"0","garment_type":"rtr","QTY":"57","core_material":"core3","created_time":"2015-04-10 04:52:28","apron_status":"0","inventory_id":"29","thumb":"http:\/\/demo.com\/test\/facility\/images\/no_photo.jpg","serial":"tyy567","apron_retire":"0"},{"apron_id":"12345jk","created_user":"2","Expiration":"2015-05-07","location":"5","nickname":"hhhhiiiii","UID_no":"gf465","Date_purchase":"2015-04-30","Batch":"tg4657","update_time":"2015-04-10 04:53:35","color":"yello7","manufacture":"0","garment_type":"thick","QTY":"89","core_material":"core4","created_time":"2015-04-10 04:53:35","apron_status":"0","inventory_id":"30","thumb":"http:\/\/demo.com\/test\/facility\/images\/no_photo.jpg","serial":"5rtgr","apron_retire":"0"},{"apron_id":"12345kl","created_user":"2","Expiration":"2015-08-14","location":"5","nickname":"hhjhj","UID_no":"rrrrrrr","Date_purchase":"2015-04-17","Batch":"hghh","update_time":"2015-04-10 05:14:57","color":"ytytyt","manufacture":"0","garment_type":"jyjjyj","QTY":"65765","core_material":"fghf","created_time":"2015-04-10 05:14:57","apron_status":"0","inventory_id":"31","thumb":"http:\/\/demo.com\/test\/facility\/images\/no_photo.jpg","serial":"fh","apron_retire":"0"},{"apron_id":"67675687866","created_user":"2","Expiration":"2018-05-08","location":"13","nickname":"tyujyhj","UID_no":"yujyhjy","Date_purchase":"2015-05-08","Batch":"ytyty","update_time":"2015-04-23 07:15:06","color":"hhjyj","manufacture":"14","garment_type":"yujyhjyu","QTY":"2147483647","core_material":"hhjh","created_time":"2015-04-10 05:27:02","apron_status":"0","inventory_id":"37","thumb":"http:\/\/demo.com\/test\/facility\/images\/no_photo.jpg","serial":"t yt","apron_retire":"0"},{"apron_id":"12345as","created_user":"2","Expiration":"2015-06-30","location":"13","nickname":"nick","UID_no":"3546re","Date_purchase":"2015-04-01","Batch":"3454fgf","update_time":"2015-04-10 04:49:10","color":"black","manufacture":"0","garment_type":"pure1","QTY":"42","core_material":"core1","created_time":"2015-04-10 04:49:10","apron_status":"0","inventory_id":"27","thumb":"http:\/\/demo.com\/test\/facility\/images\/no_photo.jpg","serial":"2323","apron_retire":"0"},{"apron_id":"89745465798416467987","created_user":"2","Expiration":"1969-12-31","location":"12","nickname":"test","UID_no":"35678"
No Listing comming because of this error
[1] 3.onErrorResponse: MainActivity
Please suggest me your views. Thanks in advance.
I have changed JSONArrayRequest
to JSONObjectRequest
New Error comes.
It's giving JSONException
because you are trying to parse the response into the JSONArray
, whereas server is returning response packaged into the JSONObject
.
JsonArrayRequest inventoryReq = new JsonArrayRequest(Request.Method.GET, url,(String) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
.............
.............
}
Handle JSONObject
in onResponse()
method.
JsonObjectRequest inventoryReq = new JsonObjectRequest(Request.Method.GET,
url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
int status = response.getInt("status");
if(status == 1){
JSONArray data = response.getJSONArray("data")
for(int i=0; i<data.length(); i++){
JSONObject obj = data.getJSONObject(i);
//Do something
}
}catch(...){
...
}
}
........ ........ ........
........ ........ ........
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With