当id超过表长度时,服务器给出null值

时间:2016-01-18 15:19:58

标签: android mysql json

这是主要活动的代码

     private void getData() {
    loading = ProgressDialog.show(this, "Please wait...", "Fetching...", false, false);
    //public static long queryNumEntries (SQLiteDatabase db, String Object table;
    //table)
    int id = 2;

代码通过递增id值来接收数据但是我们不知道何时停止它,当它从表中获取所有数据时我们得到null。This is the output in emulator

    do {


        String id1 = String.valueOf(id);
        String url = DATA_URL + id1.trim(); //+ editTextId.getText().toString().trim();

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override


            public void onResponse(String response) {

                loading.dismiss();

                showJSON(response);

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                    }

                });


RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

        id++;

    }while (id<10);
}

private void showJSON(String response) {

    try {

        //  String name="";
        //String address="";
        // String vc = "";
        // String message="";
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        name = collegeData.getString(config.KEY_NAME);
        address = collegeData.getString(config.KEY_ADDRESS);
        vc = collegeData.getString(config.KEY_VC);
        message = collegeData.getString(config.message);
        HashMap<String, String> persons = new HashMap<String, String>();

        persons.put(KEY_NAME, name);
        persons.put(KEY_ADDRESS, address);
        persons.put(KEY_VC, vc);
        persons.put(config.message, message);
        personList.add(persons);

        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, personList, R.layout.liist,
                new String[]{config.KEY_NAME, config.KEY_ADDRESS, config.KEY_VC, config.message},
                new int[]{R.id.id2, R.id.name, R.id.address, R.id.id1}

        );


        lst.setAdapter(adapter);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    //  textViewResult.setText("classname:\t"+name+"\ntime:\t" +address+ "\nfaculty\t"+ vc+"\nmessage\t"+message);
}


@Override
public void onClick(View v) {
    getData();
}

1 个答案:

答案 0 :(得分:0)

您应该在收到上一个请求的结果后添加下一个请求。

    private int id = 2;
    private void tryGetData(){
        String id1 = String.valueOf(id);
        String url = DATA_URL + id1.trim(); //+ editTextId.getText().toString().trim();

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

                loading.dismiss();

                showJSON(response);
                if(!"null".equalsIgnoreCase(response) && !TextUtils.isEnpty(response)){
                    id++;
                    tryGetData();
                }else{
                    //todo show msg "it's the end"
                }
            }},
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();

                }

            });


        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }