Получение только последнего значения из базы данных несколько раз в списке - PullRequest
0 голосов
/ 17 апреля 2019

в настоящее время использую залп и пытаюсь получить результат foreach от моего php и отправить его в мой список. проблема в том, что я продолжаю получать последнее значение 4 раза, поскольку в БД есть только два значения. также отображается только последнее значение среди этих двух, и оно отображается 4 раза

Я пытался использовать jsonObject и jsonArray, я не знаю, что делаю не так

private void loadList() {
    displayLoader();
    JSONObject request = new JSONObject();
    try {
        //Populate the request parameters

        request.put(KEY_USER, userID);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    JsonObjectRequest jsArrayRequest = new JsonObjectRequest
            (Request.Method.POST, login_url, request, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    pDialog.dismiss();

                    for (int i = 0; i < response.length(); i++) {
                        try {

                            Log.e ( "response", "" + response);
                            //JSONObject obj = response.getJSONObject(i);
                            ModelTestData test = new ModelTestData(response.getString("research")
                                    ,response.getString("name")
                                    ,response.getString("hospital")
                                    ,response.getString("type"));

                            test.setResearch(response.getString("research"));
                            test.setName(response.getString("name"));
                            test.setHospital(response.getString("hospital"));
                            test.setType(response.getString("type"));


                            // adding movie to movies array
                            testList.add(test);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    cAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    pDialog.dismiss();

                    //Display error message whenever an error occurs
                    Toast.makeText(getApplicationContext(),
                            "Olau", Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();

                    //  Toast.makeText(getApplicationContext(),
                    //      "Finally success second", Toast.LENGTH_SHORT).show();

                }
            });

    // Access the RequestQueue through your singleton class.
    MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
}

Вот как выглядит ответ

{"research":"I got this error while debugging an android app on Android Studio via USB. Connection attempts failed with one tablet but not with my regular test tablet. It turned out that the failure was due to the fact that I hadn't enabled WIFI settings on the failing machine. So I enabled WIFI on it and it connected to server OK.","name":"Pestal feel","hospital":"Saint Paule","type":"Pancrease","0":true}{"research":"I got this error while debugging an android app on Android Studio via USB. Connection attempts failed with one tablet but not with my regular test tablet. It turned out that the failure was due to the fact that I hadn't enabled WIFI settings on the failing machine. So I enabled WIFI on it and it connected to server OK.","name":"Coner stone","hospital":"Saint Paule","type":"Breast","0":true,"1":true}
...