使用凌空从多个页面加载Json数据

时间:2018-07-05 22:15:12

标签: android json parsing android-volley jsonparser

我的代码有问题,请您帮忙?

我有一个Json数据,它是一个客户端列表,我需要解析它,但是有8949页,但是它只显示第一页,EndlessScrollListener没有帮助,它始终只显示前12个客户端,并且不显示无论我尝试加载下一页。 我正在为此项目使用自定义适配器和AppController。

这是MainActivity.java代码。 公共类MainActivity扩展了AppCompatActivity {

private static
String url = "https:my_url_page=1";


private ProgressDialog dialog;
private List<Item> array = new ArrayList<Item>();
private ListView listView;
private ItemAdapter adapter;
final String LOG_TAG = "myLogs";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = findViewById(R.id.list_item);
    adapter = new ItemAdapter(this, array);
    listView.setAdapter(adapter);



    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading...");
    dialog.show();



    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            hideDialog();

            try {


                JSONObject data1 = new JSONObject(response);

                JSONObject data = data1.getJSONObject("data");

                JSONObject list = data.getJSONObject("list");


                Iterator keys = list.keys();
                while (keys.hasNext()) {
                    String currentDK = (String) keys.next();

                    JSONObject currentDV = list.getJSONObject(currentDK);
                        Item item = new Item();
                        item.setPman_id(currentDV.optString("man_id"));
                        item.setPprofile(currentDV.optString("pfl_fname"));
                        item.setPheight(currentDV.optString("height"));
                        item.setPweight(currentDV.optString("weight"));
                        item.setPprofile_about(currentDV.optString("pfl_about_me"));
                        item.setPPage(currentDV.optString("age"));
                        item.setPcity(currentDV.optString("city"));
                        item.setPcountry(currentDV.optString("country"));
                        item.setPavatar(currentDV.optString("avatar"));
                        item.setPonline(currentDV.optInt("online"));
                        item.setPmessage(currentDV.optInt("msg_on"));
                        item.setPfavs(currentDV.optInt("is_favorites"));
                        item.setPlists(currentDV.optInt("in_lists"));
                        item.setPimage(currentDV.optString("image_count"));
                        item.setPvideo(currentDV.optString("video_count"));



                        array.add(item);


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


}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    }

})

    {
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String statusCode = String.valueOf(response.statusCode);
        String message = "OK";
        String session_id = String.valueOf(TokenHandler.getToken());
        String data = "data: " + "session_id: " + session_id;

        Log.d(LOG_TAG, "code: " + statusCode + "\n" + "message: " + message + "\n" + data);
        return super.parseNetworkResponse(response);
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<>();
        headers.put("X-Token", TokenHandler.getToken());
        return headers;
    }

    @Override
    public Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("user_name", "name");
        params.put("password", "pass");
        return params;

    }

    };

    AppController.getmInstance().addToRequesQueue(stringRequest);
}




public void hideDialog(){
    if (dialog !=null){
        dialog.dismiss();
        dialog = null;

    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

0 个答案:

没有答案