0

I can get very strange issue in my project. I can get the response from volley over the internet and after reposne I want to store it in sharedpref, but issue is that when I get the response and showed up within resonse function then it shows correct data, but when I used to save it outside the response function sharedpref it gives 0. I declared the string public and top of the class but got no luck. Am very strange whats the issue.

SharedPreferences savelogin = getSharedPreferences("login",MODE_PRIVATE);
                                final SharedPreferences.Editor slogin = savelogin.edit();


                                String url = "https://datafinderdatabase.xyz/dfapi/FetchId.php?username="+fuser;
                                StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                       userid = response.toString();

                                    }
                                }, new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        Toasty.error(getApplicationContext(), "Network Issue", Toast.LENGTH_SHORT).show();
                                    }
                                });

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

                                slogin.putString("username",fuser);
                                slogin.putString("password",fpass);
                                slogin.putString("userid",userid);
                                slogin.commit();

2 Answers2

0

This is because your call to the API is asynchronous. Therefore, the result you get back may be processed after that function finishes. To solve this, you can take a Interface approach, explained here for a similar issue: wait until firebase retrieves data

OR: you need to save the variable to the shared preferences inside the onResponse function.

Sinan Yaman
  • 5,714
  • 2
  • 15
  • 35
0

These:

slogin.putString("username",fuser);
slogin.putString("password",fpass);
slogin.putString("userid",userid);
slogin.commit();

must be inside this:

@Override
public void onResponse(String response) {
userid = response.toString();
//here

}
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • I'll put this lines inside the onResponse then it worked fine but it generates another issue. After login it can not shows sharedprefs data until Home activity off and then on again. – Muhammad Usama Apr 21 '20 at 17:52
  • I don't understand what you mean, if you want the data to be the saved the only guarantee is to put them like I told you, any thing else is another question. – Hasan Bou Taam Apr 21 '20 at 17:55