如何在JSONArray中使用daimajia / AndroidImageSlider库?

时间:2015-11-17 20:35:55

标签: android json

我希望这能实现,所以如果我想在将来改变一些东西。 我在那里,我可以用json做到这一点。在这个库的创建者的例子中,他展示了我们如何使用图像的特定url链接来做到这一点。

我正在使用的

LINK

有什么建议吗?

这是我到目前为止所做的:

public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
    public ProgressDialog pDialog;
    JSONObject jsonResponse;
    ArrayList arraylist = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(EditBirthdayActivity.this);
        pDialog.setIndeterminate(false);
        pDialog.setMessage("Loading, Please wait...");
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        Integer result = 0;
        HttpURLConnection urlConnection;

        try {
            URL url = new URL(params[0]);

            urlConnection = (HttpURLConnection) url.openConnection();
            /* for Get request */
            urlConnection.setRequestMethod("GET");

            int statusCode = urlConnection.getResponseCode();

            /* 200 represents HTTP OK */
            if (statusCode ==  200) {

                BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    response.append(line);
                }

                parseResult(response.toString()); // here i'm getting an error 

                result = 1;

            }else{

                result = 0; //"Failed to fetch data!";

            }

        } catch (Exception e) {
            Toast.makeText(EditBirthdayActivity.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
        }

        return result; //"Failed to fetch data!";
    }

    @Override
    protected void onPostExecute(Integer result) {

        try {
            jsonResponse = new JSONObject(result); // here i'm getting an error on passing result
            JSONArray jsonMainNode = jsonResponse.optJSONArray("Image-Slider");

            int lengthJsonArr = jsonMainNode.length();
            for(int i=0; i < lengthJsonArr; i++)
            {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);


                map.put(jsonChildNode.getString("Title"), jsonChildNode.getString("Image"));
                arraylist.add(map); 
                for(String name : map.keySet()){
                    TextSliderView textSliderView = new TextSliderView(EditBirthdayActivity.this);

                    textSliderView
                            .description(name)
                            .image(map.get(name))
                            .setScaleType(BaseSliderView.ScaleType.Fit);

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

        } catch (JSONException e) {

            e.printStackTrace();
        }
    }
}

JSON网址链接:

"Image-Slider": [
{

"Image": "http://url/Main1.jpg",
"Title": "title 1"
},
{
"Image": "http://url/Main2.jpg",
"Title": "title 2"
},
{
"Image": "http://url/Main2.jpg",
"Title": "title 3"
},
{
"Image": "http://url/Main3.jpg",
"Title": "title 4"
}


]

1 个答案:

答案 0 :(得分:0)

我认为你的逻辑存在错误。导致此代码

for(String name : map.keySet()){
                    TextSliderView textSliderView = new TextSliderView(EditBirthdayActivity.this);

                    textSliderView
                            .description(name)
                            .image(map.get(name))
                            .setScaleType(BaseSliderView.ScaleType.Fit);

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

必须放在for(int i=0; i < lengthJsonArr; i++)

之外

所以你的代码必须像这样

 @Override
    protected void onPostExecute(Integer result) {

        try {
            jsonResponse = new JSONObject(result); // here i'm getting an error on passing result
            JSONArray jsonMainNode = jsonResponse.optJSONArray("Image-Slider");

            int lengthJsonArr = jsonMainNode.length();
            for(int i=0; i < lengthJsonArr; i++)
            {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);


                map.put(jsonChildNode.getString("Title"), jsonChildNode.getString("Image"));
                arraylist.add(map); 

            }
             for(String name : map.keySet()){
                    TextSliderView textSliderView = new TextSliderView(EditBirthdayActivity.this);

                    textSliderView
                            .description(name)
                            .image(map.get(name))
                            .setScaleType(BaseSliderView.ScaleType.Fit);

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

        } catch (JSONException e) {

            e.printStackTrace();
        }
    }