0

when i am calling my function with the parameter (URL and prams) its not returning login inside the result in main activity. its returning null then it implement the toast and then it continues and the implement my function.

please any help to let the function be the first one in the implementation

public class MainActivity extends AppCompatActivity {
private String URLline = "https://demonuts.com/Demonuts/JsonTest/Tennis/simplelogin.php";

    private EditText etUname, etPass;
    private Button btn;
    public static String firstName, hobby,myresponse,myresponse2,result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etUname = findViewById(R.id.etusername);
        etPass = findViewById(R.id.etpassword);
        btn = findViewById(R.id.btn);
        final Map<String,String> params2 = new HashMap<String, String>();
        params2.put("PARAM1","AAA");
        params2.put("PARAM2","ABC");


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result=loginUser(URLline,params2);
                Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG).show();
                        Toast.makeText(MainActivity.this,"Hellooooooo",Toast.LENGTH_LONG).show();

            }
        });

    }
    private String loginUser(String URL, final Map params2){

            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,

                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                       Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                        myresponse =response;
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                }

                )

        {
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.putAll(params2);
                return params;
            }
        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(200000, -1, 0));
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        if (myresponse != null)
        {
            myresponse=myresponse.replaceAll("//r//n","");
            myresponse=myresponse.replaceAll("//","");
            myresponse=myresponse.substring(1,myresponse.length()-2);
        }
        return myresponse;
    }

}
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

Volley doesn't return values you have to check for other approaches for this like directly show the toast in the function where you are getting the response from API as like mentioned below:

new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                        myresponse =response;
**/////get all the data here and show that into the response.**
                    }
                },
  • hello , sorry i think you understand me wrong.. the problem is when debugging , the on response code is being implemented but there is a delay it is returning the var but its empty , by that i want to let the function done and then return the var.. – Rajaa Khalifeh May 19 '19 at 15:54
  • Yes you will face this delay because may be when you will debug the code then when you will hit the api then debugger goes to the last lines of api code and will assign the value to your variable null by default and then it will not change that – Rajat Kumar Tyagi May 19 '19 at 15:57
  • I have tried this thing once but it didn't worked for me too so that is why i am telling you and if you will solved this one then also let me know. – Rajat Kumar Tyagi May 19 '19 at 15:59