简体   繁体   English

Android JSON解析器加载更多数据

[英]android json parser load more data

Because my json source is sometimes too long, and my android app crashes if it takes too long to load, i want to modify my code, so that when the user scrolls down to automaticaly load data or if is more simple, i would ike to add a button "Load More". 因为我的json源有时会太长,并且如果加载时间太长,我的android应用程序会崩溃,所以我想修改我的代码,以便当用户向下滚动以自动加载数据时,或者如果更简单,我会喜欢添加一个按钮“加载更多”。 Here is my existing code. 这是我现有的代码。 What is the part i must modify for adding autoload or maybe a "Load More" button? 添加自动加载或“加载更多”按钮时,我必须修改什么部分? Thanks! 谢谢!

package com.radioxxx.aacplay;

import android.app.DownloadManager;
import android.app.Fragment;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FazeNoi extends ListFragment {
    JSONParser2 jsonParser = new JSONParser2();


    private ProgressDialog pDialog;
    ArrayList<HashMap<String, String>> tracksList;

    JSONArray albums = null;

    String nume_piesa, cale;
    String titlu;

    private static final String URL_ALBUMS = "http://www.radioxxx.ro/json-news";
    private static final String TAG_TITLU = "Titlu";
    private static final String TAG_POZA = "FazeNoiID";
    private static final String TAG_NUME = "CMSdate";
    private static final String TAG_CALE = "URL";



    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        tracksList = new ArrayList<HashMap<String, String>>();

        new LoadTracks().execute();



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_fazenoi, container, false);

        return rootView;
    }


    class LoadTracks extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Se incarca ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            //params.add(new BasicNameValuePair(TAG_ID, album_id));

            String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
                    params);

            Log.d("Track List JSON: ", json);

            try {
                JSONArray albums = new JSONArray(json);



                for (int i = 0; i < albums.length(); i++) {
                    JSONObject c = albums.getJSONObject(i);
                    //String poza = c.getJSONArray("PozaPrincipalaMedia").getJSONObject(0).getString("thumb");
                    String track_no = String.valueOf(i + 1);
                    String poza = c.getJSONObject("PozaPrincipalaMedia").getString("thumb");
                    String idpoza = c.getJSONObject("PozaPrincipalaMedia").getString("id");
                    String nume = c.getString(TAG_TITLU);
                    String cale = c.getString(TAG_CALE);



                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("track_no", track_no + ".");

                    map.put(TAG_POZA, poza);
                    map.put(TAG_NUME, nume);
                    map.put(TAG_CALE, cale);

                    tracksList.add(map);
                }



            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    ListAdapter adapter = new SimpleAdapter(
                            getActivity(), tracksList,
                            R.layout.list_faze, new String[] {TAG_POZA, "track_no", TAG_NUME }, new int[] {
                            R.id.list_image,
                            R.id.track_no, R.id.nume_piesa });

                    setListAdapter(adapter);
                    //TextView text = (TextView) getActivity().findViewById(R.id.version);
                    //text.setText(titlu);
                }
            });

        }

    }


}

I'm going to post my comment as an answer, I believe it's the easiest way to quickly solve your problem. 我将发表评论作为答案,我相信这是快速解决您的问题的最简单方法。 If you can control how much JSON is being sent on the server side, then make requests from your java app for page 1, page 2 etc. 如果您可以控制在服务器端发送多少JSON ,请从Java应用程序发出第1页,第2页等的请求。

Page 1,page 2,etc... could just send out small portions of the JSON you need, then attach a listener (button/scrollview) to request the next page, so on an so forth. 页面1,页面2等...可以只发送所需的JSON小部分,然后附加一个侦听器(按钮/滚动视图)以请求下一页,依此类推。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM