使用AsyncTask在daimajia-AndroidImageSlider中使用jsonData

时间:2015-06-14 13:27:45

标签: android json android-asynctask

我使用带有三张图片的滑块的https://github.com/daimajia/AndroidImageSlider。这是我在AsyncTask尝试加载三张图片来自{ {1}}:

Json Data

当然要加载,我们需要在public class AsyncHttpTask extends AsyncTask<String, Void, Integer> { public ProgressDialog pDialog; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MainActivity.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 { /* forming th java.net.URL object */ 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()); result = 1; }else{ result = 0; //"Failed to fetch data!"; } } catch (Exception e) { Toast.makeText(MainActivity.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); } return result; //"Failed to fetch data!"; } @Override protected void onPostExecute(Integer result) { /* Download complete. Lets update UI */ if (result == 1) { adapter = new MyRecyclerAdapter(MainActivity.this, feedItemList); mmRecyclerView.setAdapter(adapter); pDialog.dismiss(); } else { Toast.makeText(MainActivity.this, "Failed to fetch data!", Toast.LENGTH_SHORT).show(); } } } 上使用它:

Oncreate

所以,我的final String url = "http://url/Data.json"; //json data url new AsyncHttpTask().execute(url); //execute the json data 中有三张图片:

JsonData

//我的JsonData正在解决任何问题。但是,

如何在此滑块中显示这三个图像,如上所述"Main-header": [ { "HeaderImg1": "http://url/Main1.jpg", "Title": "title 1" }, { "HeaderImg2": "http://url/Main2.jpg", "Title": "title 2" }, { "HeaderImg3": "http://url/Main2.jpg", "Title": "title 3" } ]

编辑:以下是我在Oncreate上完成的操作,这个,工作正常,但我需要从Json数据加载:

JsonData

任何帮助感谢。

干杯!

1 个答案:

答案 0 :(得分:2)

这就是你应该怎么做的。在Asynctask的onPostExecute方法中使用此代码。

                arraylist = new ArrayList<HashMap<String, String>>();
                JSONObject jsonResponse;
                try {
                    jsonResponse = new JSONObject(result);
                    JSONArray jsonMainNode = jsonResponse.optJSONArray("Main-header");

                    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("HeaderImg2"));
                        arraylist.add(map);
                        for(String name : map.keySet()){
                            TextSliderView textSliderView = new TextSliderView(MainActivity.this);

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

                            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);
                        mDemoSlider.addOnPageChangeListener(MainActivity.this);
                    }


                } catch (JSONException e) {

                    e.printStackTrace();
                }


            }