mansi mervana mansi mervana - 5 months ago 13
Android Question

Image is parsing but not displaying all images in android using daimajia library

this is my code

private class DownloadJSON extends AsyncTask<Void,Void,Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ShopDeals.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.show();

}

@Override
protected Void doInBackground(Void... params) {
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JsonParser
.getJSONfromURL("http://gpstracker.mpiricsoftware.com/lopez/feed.php");

try {
// Locate the array name in JSON
//jsonarray = jsonobject.getJSONArray("worldpopulation");

jsonarray = new JSONArray(jsonobject);

for (int i = 0; i < jsonarray.length(); i++) {
//map = new HashMap<String, String>();
JSONObject jobj = (JSONObject) jsonarray.get(i);

JSONObject jsonObject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("img","http://gpstracker.mpiricsoftware.com/lopez/uploads/"+jobj.getString("image"));
Log.e("img",jobj.getString("image"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {


for (String name : map.keySet()){
TextSliderView textSliderView = new TextSliderView(ShopDeals.this);
// initialize a SliderLayout
textSliderView
.description(name)
.image(map.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(ShopDeals.this);

//add your extra information
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",name);


mDemoSlider.addSlider(textSliderView);
}
mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
mDemoSlider.setCustomAnimation(new DescriptionAnimation());
mDemoSlider.setDuration(4000);

/*listview = (ListView) findViewById(R.id.listview);
adapter = new SlidingImage_Adapter(ShopDeals.this, arraylist);
listview.setAdapter(adapter);*/

pDialog.dismiss();

}
}


this is my logcat error:

06-20 14:45:20.795 2706-2983/evip.gohybrid.com.evip E/log_tag: Error parsing data org.json.JSONException: Value [{"id":"7","song":"pop.mp3","image":"wordpress-banner.jpg","desc1":"Sermon1","desc2":"Sermon1","desc3":"Sermon1","desc4":"Sermon1"},{"id":"9","song":"1.png","image":"1.png","desc1":"catb","desc2":"cabt","desc3":"cad","desc4":"dsad"}] of type org.json.JSONArray cannot be converted to JSONObject
06-20 14:45:20.797 2706-2983/evip.gohybrid.com.evip E/img: wordpress-banner.jpg
06-20 14:45:21.004 2706-2983/evip.gohybrid.com.evip E/img: 1.png


can you please tell whats the problem..?

Answer

Try this

  @Override
            protected Void doInBackground(Void... params) {
                arraylist = new ArrayList<HashMap<String, String>>();
                // Retrieve JSON Objects from the given URL address
               String  jsonobject = JsonParser
                        .getJSONfromURL("http://gpstracker.mpiricsoftware.com/lopez/feed.php");

                try {
                    // Locate the array name in JSON
                    //jsonarray = jsonobject.getJSONArray("worldpopulation");

                   JSONArray jsonarray = new JSONArray(jsonobject);

                    for (int i = 0; i < jsonarray.length(); i++) {

                        JSONObject jsonObject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        map.put(String.valueOf(i),"http://gpstracker.mpiricsoftware.com/lopez/uploads/"+jsonObject.getString("image"));
                        Log.e(String.valueOf(i),jsonObject.getString("image"));
                        // Set the JSON Objects into the array
                        arraylist.add(map);
                    }
                } catch (JSONException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;
            }

and in postbackground

 @Override
            protected void onPostExecute(Void aVoid) {

                int i=0;
                for (String name : map.keySet()){
                    TextSliderView textSliderView = new TextSliderView(ShopDeals.this);
                    // initialize a SliderLayout
                    textSliderView
                            .description(name)
                            .image(map.get(String.valueOf(i)))
                            .setScaleType(BaseSliderView.ScaleType.Fit)
                            .setOnSliderClickListener(ShopDeals.this);

                    //add your extra information
                    textSliderView.bundle(new Bundle());
                    textSliderView.getBundle()
                            .putString("extra",name);


                    mDemoSlider.addSlider(textSliderView);
                    i++;
                }
                mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
                mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
                mDemoSlider.setCustomAnimation(new DescriptionAnimation());
                mDemoSlider.setDuration(4000);

    /*listview = (ListView) findViewById(R.id.listview);
    adapter = new SlidingImage_Adapter(ShopDeals.this, arraylist);
    listview.setAdapter(adapter);*/

                pDialog.dismiss();

            }

chnage code and try it

Comments