这是我的代码
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();
}
}
这是我的logcat错误:
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
你能告诉我们这个问题吗??
答案 0 :(得分:1)
试试这个
@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;
}
和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代码并试一试
答案 1 :(得分:0)
您在doInBackground
:
jsonobject = JsonParser.getJSONfromURL("http://gpstracker.mpiricsoftware.com/lopez/feed.php");
如果jsonobject
是JSONObject
,则表示您的错误。响应的根源是JSONArray
。
如果您将jsonobject
更改为JSONArray
类型,则应该通过。