A
A
Alexander Lee2021-04-15 18:06:35
Android
Alexander Lee, 2021-04-15 18:06:35

How to display the error body after a post request?

I should (and receive) a response with a 302 code. I need to display the received response, but body () displays only successful requests, which it will not consider mine. I wanted to use errorBody (), but it is highlighted in red (the reason is not known to me).
How to use errorBody() in my case? Or are there any analogues?

final OkHttpClient client = new OkHttpClient()
            .newBuilder()
            .followRedirects(false)
            .followSslRedirects(false)
            .build();
    class AsyncRequest extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... arg) {

            RequestBody form = new FormBody.Builder()
                    .add("studentBookId", "Номер зачётной книги")
                    .addEncoded("f","Фамилия")
                    .addEncoded("i","Имя")
                    .addEncoded("pass","Пароль")
                    .add("class","pwd")
                    .add("submit","Войти")
                    .build();

            Request request = new Request.Builder()
                    .url(URL_Base)
                    .post(form)
                    .build();

            Call call = client.newCall(request);
            Response response = null;
            try {
                response = call.execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Lee, 2021-04-19
@SashaDown

I found the reason: I do not receive the body, since I need to read the cookie and resend the request accordingly, and there I will receive the necessary file

D
Denis Zagaevsky, 2021-04-15
@zagayevskiy

In theory, the erroneous body will also be in body (). Have you checked, or is this your fabrication?
OkHttp knows how to make asynchronous requests itself, you don't need to use asynctask for this. Forget about its existence at all, and never use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question