Q:

HTTP POST Request as a different Java Class - Android Studio

I am a complete noob to Android Studio, Java and Stack Overflow. My app performs a lot of HTTP Post requests using Volley and hence I've made an independent Java class with the code to perform the post request.
public class HTTPReq {
    String[] finalResponse = new String[1];
    public String postRequest(final  HashMap<String,String> params, final Context context) {
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        String url = "https://reqres.in/api/login";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                finalResponse[0] = response;
                Toast.makeText(context, "2" + finalResponse[0], Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                finalResponse[1] = error.getMessage();
                //Toast.makeText(context, "Response Failed", Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };
        requestQueue.add(stringRequest);
        Toast.makeText(context, "3" + finalResponse[0], Toast.LENGTH_LONG).show();
        return finalResponse[0];
    }
}

What I'm trying to achieve is to get the response of the http request to the function call using return.
The function call is as follows:
public void login(String phno, String password,Context context)
{
    HashMap<String,String> credentials = new HashMap<String, String>();
    credentials.put("email","eve.holt@reqres.in");
    credentials.put("password","cityslicka");
    HTTPReq httpReq = new HTTPReq();
    String response = httpReq.postRequest(credentials,context);
    Toast.makeText(context, "1" + response, Toast.LENGTH_LONG).show();
}

I hope it's clear what I'm trying to achieve. Please help me with this.

A:

your con use feature interface
   public class HTTPReq {
    public void postRequest(final HashMap<String, String> params, final Context context, final ResponseCallBack callBack) {
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        String url = "https://reqres.in/api/login";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                callBack.onResponse(response);
                Toast.makeText(context, response, Toast.LENGTH_LONG).show();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callBack.onError(error);
                Toast.makeText(context, "Response Failed", Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };

        requestQueue.add(stringRequest);

    }
}

calss interface :
import com.android.volley.VolleyError;

public interface ResponseCallBack<T> {
        public void onResponse(T response);

        public void onError(VolleyError error_response);

}

MainActivity:
public class MainActivity extends AppCompatActivity implements ResponseCallBack {

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

    HTTPReq httpReq = new HTTPReq();
    HashMap<String, String> credentials = new HashMap<String, String>();
    credentials.put("email", "eve.holt@reqres.in");
    credentials.put("password", "cityslicka");

    httpReq.postRequest(credentials, this, this);

}

@Override
public void onResponse(Object response) {
    Log.e("TAG", "onResponse: " + response);
}

@Override
public void onError(VolleyError error_response) {

    Log.e("TAG", "onError: " + error_response);
}

