1

i have the following method inside mainActivity but i am trying to move it to separate class file so i can call it in other activities

public void addUser(){


    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            constants.register_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(getApplicationContext(), response ,Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), error.getMessage() ,Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            params.put("username", "value");
            params.put("email", "value");
            params.put("password", "value");
            return params;
        }
    };

    requestHandler.getInstance(this).addToRequestQueue(stringRequest);
}

i have created a class called 'aqlCalls' and tried to put this and other methods inside but i can't figure how to do that.. any help appreciated..

Omar Alhadidy
  • 109
  • 1
  • 9

3 Answers3

1

You can create your own listener to communicate with the activities and set the method static and save inside a class, passing the Context and use it in the request call.

public static void addUser(Context context, AddUserListener listener){
...
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                listener.onSucces(response);
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                listener.onError(error.getMessage())
            }
        })

The Listener should be:

public interface AddUserListener{
    void onSuccess(String response);
    void onError(String message)
}

And finally in the activity do this:

MyClass.addUser(getApplicationContext(), new AddUserListener {
   @Override
   void onSuccess(String response){
      ... //Toast.makeText(...)
   }
   @Override
   void onError(String message){
      ...//Toast.makeText(...)
   }
});

Notice: Please if you receive any Exception write down in your post, maybe you will receive an exception related with MainThread

AndroidStorm
  • 859
  • 9
  • 25
1

I think you need to try to find the exact problem firstly.

According to the code in your method, I guess the problem is the Toast. If you want to use the Toast outside the Activity, you need to pass the context to this method. If you search for "android toast outside activity", you will get a lot of similar questions. such as this.

I suggest that adding a new parameter to context the addUser() and use the context to show the Toast.

And when using this method, pass a context (such as getApplicationContext()) to the method.

for example

void addUser(Context mcontext){
   //all the toast usage change to 
   Toast.makeText(mContext, ....)
}
Xin Meng
  • 1,982
  • 3
  • 20
  • 32
0

I think your looking like something like this:

public class aqlCalls {

   public static void addUser(Context context) {
       StringRequest stringRequest = new StringRequest(Request.Method.POST, constants.register_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("username", "value");
            params.put("email", "value");
            params.put("password", "value");
            return params;
        }
       };
       requestHandler.getInstance(context).addToRequestQueue(stringRequest);
   }
}

Then you can call it from your class with:

public class OtherClass{
   static void yourMethode() {
      aqlCalls.addUser();
   }
}

I think this is what you are looking for.

Developer66
  • 732
  • 6
  • 17