F
F
fanhypermax2018-11-16 00:47:08
Java
fanhypermax, 2018-11-16 00:47:08

How to pass parameters to doInBackground?

Sender handler = new Sender();
            FormBody.Builder params= new FormBody.Builder();
            params.add("param1", "val");
            params.add("param2", "val");
            result = handler.execute("https://domain.ru/",params).get();

How to make arg[0] be url and arg[1] be parameters ?
public class Sender extends AsyncTask<String, FormBody.Builder, String> {
    private static final OkHttpClient client = new OkHttpClient();

        protected String doInBackground(String... arg) {

            try {

                RequestBody formBody = arg[1].build();

                Request request = new Request.Builder()

                        .url(arg[0])
                        .post(formBody)
                        .build();

                Response response = client.newCall(request).execute();

                return response.body().string();

            } catch (Exception e) {
                e.printStackTrace();
            }
            return "fail";
        }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor, 2018-11-16
@RATlius

In your case, you are not declaring the class correctly. You should have it like
And this article

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question