How to get a response from the POST request, thanks in advance. Call in Maine:
new DoPOSTInBackground().execute(key) String[] key = {"qwerty"}; The class itself:
package com.admin.post; import android.os.AsyncTask; import java.io.IOException; import java.util.ArrayList; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; class DoPOSTInBackground extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... keys) { OkHttpClient client = new OkHttpClient(); RequestBody formBody = new FormBody.Builder() .add("name_form", "key_client") .add("key", keys[0]) .build(); Request request = new Request.Builder() .url("http://example.ru/index.php") .post(formBody).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { System.out.println(response.body().string()); } }); return null; } }
response().body().string(), no? - Mansur Nashaev