How to pass multiple primitive parameters to AsyncTask

Hey,
Sometimes passing a parameter is really hard on AsyncTask. Especially you want to pass multiple primitive as parameters.
Firstly, you need a POJO that will assist you to keep your primitive parameters.
Here is the example of a POJO for AsyncTask :
public class MyTaskParams {
    String method;
    String username;
    String password;
    String email;
    String gender;
    int age;
    String country;
    int daily_goal;
    String user_login;

    public MyTaskParams(String method, String username, String password, String email, String gender, int age, String country, int daily_goal) {
        this.method = method;
        this.username = username;
        this.password = password;
        this.email = email;
        this.gender = gender;
        this.age = age;
        this.country = country;
        this.daily_goal = daily_goal;
    }

    public MyTaskParams(String method,String user_login, String password){
        this.method = method;
        this.user_login = user_login;
        this.password = password;
    }


}

Now, let’s adapt this pojo to AsyncTask.
public class BackgroundTask extends AsyncTask<MyTaskParams,Void,String> {

    Context context;
    public BackgroundTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(MyTaskParams... params) {
       
        String method             = params[0].method;
        if(method.equals("Register")) {
            String username = params[0].username;
            String password = params[0].password;
            String email = params[0].email;
            String gender = params[0].gender;
            int age = params[0].age;
            String country = params[0].country;
            int daily_goal = params[0].daily_goal;
            Log.d("MyApp", "**** Information ***** \n" + username + "\n" + password + "\n" + email + "\n" + gender + "\n" + age + "\n" + country + "\n" + daily_goal);
        }
    }
}



Remember that, this is just an example of AsyncTask with multiple parameters. You need to transform these code segments on your own code :)
Tip : You should not forget while you are using extra pojo for AsyncTask, your params object contains each value inside so you need to get all with “params[0]”.. Do not confuse with : params[0],params[1] for each value. You just need to use params[0].valuename that’s all :)
If you have a question, ask me :)

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...