0

I am working on chatbot which uses API to get data but when i make request in start it is working good but if i want to make another request , the result is still the old response from old request, which i have to send request again to get new result. Is there any solution ????

i tried wait() in function that have volley requests but it does not work

   public String getResult(String team1,String team2,String code,Context context)
   {
    this.context=context;

    //"https://apifootball.com/api/?action=get_H2H&firstTeam=Arsenal&secondTeam=Chelsea&APIkey=7"

    String URL="https://apifootball.com/api/?action=get_H2H&firstTeam="+team1+"&secondTeam="+team2+"&APIkey=7";
    //"https://apifootball.com/api/?action=get_countries&APIkey=7";
    RequestQueue rq= Volley.newRequestQueue(context);
    JsonObjectRequest objreq= new JsonObjectRequest(

            Request.Method.GET,
            URL,
            null,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response) {
                    String Scores="";

                    //    Log.e("result:",response.get(0).toString());
                    JSONObject obj;

                    //  obj=response.getJSONObject("firstTeam_VS_secondTeam");
                    try {

                        JSONArray obj2 =response.getJSONArray("firstTeam_VS_secondTeam");
                        Log.e("obj", obj2.getJSONObject(0).getString("match_hometeam_score"));
                        Scores=Scores+ obj2.getJSONObject(0).getString("match_hometeam_score")+"\n"+obj2.getJSONObject(0).getString("match_awayteam_score")+"\n"+obj2.getJSONObject(0).getString("match_date");

                    } catch (JSONException e) {

                    }
                    String []arr = Scores.split("\n");
                    model = new ChatModel("First team:"+arr[0]+"\nSecond team:"+arr[1]+"\n"+"Date:"+arr[2], false);
                    list_chat.add(model);

                    //share(Scores);



                }


            },
            new Response.ErrorListener(){

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("rest response",error.toString());






                }
            }


    );
    rq.add(objreq);


    SharedPreferences m= PreferenceManager.getDefaultSharedPreferences(context);
    final String resp=m.getString("Response","");
    return  resp;
}

main activity

 if(result.equals("error")==true) {

                    APIAdapter ap = new APIAdapter();
                    head2Head = ap.getResult("Bristol City", "Reading", "kjkn", getApplicationContext());
                    finres = head2Head;
                    Log.e("headto",head2Head);
                    arr = head2Head.split("\n");
                    //send(arr[2],false);

                    // model = new ChatModel("First team:"+arr[0]+"\nSecond team:"+arr[1]+"\n"+"Date:"+arr[2], false); // user send message
/*
                    Team t1=new Team(3,"Bristol City");
                    Team t2=new Team(0,"Reading");
                    Long tid1=x.insertTeam(t1);
                    Long tid2=x.insertTeam(t2);

                    Match m=new Match(0,Integer.parseInt(String.valueOf(tid1)),Integer.parseInt(String.valueOf(tid2)),arr[2]);
                    Long mid=x.insertMatch(m);
                    Log.e("mid",String.valueOf(mid));
                    Result resul=new Result(0,Integer.parseInt(String.valueOf(mid)),x.getTeam(tid1).getTeamId(),x.getTeam(tid2).getTeamId(),Integer.parseInt(arr[0]),Integer.parseInt(arr[1]));
                    x.insertResult(resul);

                */}
                send("First team:"+arr[0]+"\nSecond team:"+arr[1]+"\n"+"Date:"+arr[2], false);


            }

send()

    void send(String text,boolean sender)
{


    ChatModel model = new ChatModel(text,sender); // user send message
    list_chat.add(model);



    CustomAdapter adapter = new CustomAdapter(list_chat,getApplicationContext());
    listView.setAdapter(adapter);

    //remove user message
    editText.setText("");
}
A.Hamza
  • 11
  • 7

1 Answers1

1

I am not sure if i understand what your problem is but i will do my best to help.

  1. Define RequestQueue outside of the function(onCreate() can be a good place), this way you don't init it every time you make a request and it can actually work as a Queue for requests.

  2. There might be a problem with the way you handle list_chat, please post the code that you use to display it.

Dor
  • 657
  • 1
  • 5
  • 11
  • i return response string to main activity – A.Hamza Jun 10 '19 at 16:54
  • in send(), You don't need to create Adapter and set it every time... you can use adapter.notifyDataSetChanged(); and it will refresh itself according to the list your provided. at onResponse(), Can you check if you get the same response every time? or it changes? (I am trying to figure out where the bug is) – Dor Jun 10 '19 at 17:06
  • problem here if i want to send another request from another function , i have to click button twice (call function twice) because in the first time it show old message – A.Hamza Jun 10 '19 at 17:09
  • Volley use different thread to manage the requests. by the way your code is built, when you activate the function getResult() it will return what ever is inside the SharedPreferences , So the problem in my opinion is that the request havnt finished it work so it didn't update SharedPreferences with new data. You can u use listeners and events to wait for the onResponse() to happen and pass the results. – Dor Jun 10 '19 at 17:23
  • how can i do this ? – A.Hamza Jun 10 '19 at 17:32
  • It's a different question from what you asked in the post. here is a tutorial https://guides.codepath.com/android/Creating-Custom-Listeners and some one already answered this question here: https://stackoverflow.com/a/8293106/9233991 – Dor Jun 10 '19 at 17:41