0

I am trying to implement a login feature for my android app, where I send an email and a password to the server. In the response I get some values which I then need to continue the app.

The problem is that I want to implement the request in a "requests class" like this:

public class MyRequests {
    public void loginToServer(String email, String password, Context context, Runnable myRunnable) {
        RequestQueue queue = Volley.newRequestQueue(context);

        StringRequest request = new StringRequest(
                Request.Method.POST,
                myURL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        myRunnable.run();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast t = Toast.makeText(context, "Error!", Toast.LENGTH_LONG);
                        t.show();
                    }
                }
        ); // Theres also some post-params and authentication here
    }
}
public class mainActivity extends AppCompatActiviy {
    Runnable myRunnable = new Runnable() {
        @Override
        public void run() {
            // continue to a new activity + do some database stuff
        }
    }
    MyRequests.loginToServer(email, password, this, myRunnable);
}

I can not figure out how to pass parameters back to my runnable so that I know for example, whether or not the call was successful, etc.

Any good ideas or good practices that I can use here?

Thanks.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Developer
  • 736
  • 7
  • 30
  • Sounds a bit overcomplicated. Why can't you just use the basic httppost style of sending and receiving server requests (Example: http://androidexample.com/How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=64) – Totumus Maximus Feb 06 '17 at 13:38
  • I think you might be right. But how would you then go about sending the response back to the activity where you called the method? I cannot simply make the method return a string since the request has to be run in for example an AsynchTask? – Developer Feb 06 '17 at 14:37
  • 1
    In the onPostExecute you can make a callback method to the Activity so you can give it feedback at the end of the execution without bothering the UIThread until it is absolutely needed. To do this check the next example: http://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui – Totumus Maximus Feb 06 '17 at 14:52

0 Answers0