title: AsyncTask in Android
body: <p>I have a client Class that connects to the server and sends a POST request and in response gets a Json answer. </p>
  
  <pre><code>public class ClientLocal extends AsyncTask&lt;Void, Void, String&gt; {
  
  private static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");  //Media type for request
  
  
  public JSONObject all_info_json;
  
  private String page;
  
  private SharedPreferences prefs;
  
  private String login;
  private String pass;
  private StringBuilder request_body = new StringBuilder();
  private OkHttpClient client = new OkHttpClient();
  
  protected String doInBackground(Void... params) {
      RequestBody body = RequestBody.create(JSON, request_body.toString());
      Request request = new Request.Builder()
              .url("https://program.yousystem.com.ua/frontend/api/user/login")
              .post(body)
              .build();
      try (Response response = client.newCall(request).execute()) {
          page = response.body().string();
          //Log.i("PAGE", "page from try" + page);
          return page;
  
      } catch (IOException e) {
          Log.i("ERROR", "1" + e.toString());
          e.printStackTrace();
          return null;
      }
  
  }
  
  
  protected void onPostExecute(String page) {
  
      try {
          all_info_json = (JSONObject) new JSONTokener(page).nextValue();
  
          JSONObject response = all_info_json.getJSONObject("response");
          Log.i("PAGE", "RESP" + response.toString());
  
          String token = response.getString("token");
          Log.i("PAGE", "TOKEN " + token);
  
          JSONObject profile = response.getJSONObject("profile");
          Log.i("PAGE", "PROFILE" + profile.toString());
  
          JSONArray balances = profile.getJSONArray("balances");
          JSONObject balance_obj = balances.getJSONObject(0);
          String balance = balance_obj.getString("balance");
          Log.i("PAGE", "Balance" + balance);
  
          JSONObject person = profile.getJSONObject("person");
          String firstName = person.getString("firstName");
          String lastName = person.getString("lastName");
          String mobile = person.getString("mobile");
          String email = person.getString("email");
  
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      //  Log.i("PAGE", "page from post" + page);
  
  }
  
  ClientLocal(String login, String pass) {
      this.login = login;
      this.pass = pass;
      this.request_body.append("{username: \"");
      this.request_body.append(login);
      this.request_body.append("\", pass: \"");
      this.request_body.append(pass);
      this.request_body.append("\", lng: \"ua\", prgCode: \"prg1\"}\"");
      Log.i("", "page constructor" + request_body.toString());
  
  }}
  </code></pre>
  
  <p>I need to execute all requests in one Client Class using AsyncTask, can anyone help me, how to do this? Because to run a request I use <code>ClientLocal client = new  ClientLocal("login","pass");</code> and after that <code>client.execute();</code>
  In my opinion I need to create Request builder class. But how to use it in Client local? </p>
  
  <pre><code>public class RequestBuilder {
  
  final private static String login_string = "https://program.yousystem.com.ua/frontend/api/user/login";
  final private static String about_me = "https://program.yousystem.com.ua/frontend/api/user/me";
  final private static String about_me_edit = "https://program.yousystem.com.ua/frontend/api/user/save";
  final private static String transaction_list = "https://program.yousystem.com.ua/frontend/api/transaction/list";
  
  String card_number;
  String password;
  
  StringBuilder request_body_login = new StringBuilder();
  
  public RequestBuilder(String card_number_, String password_) {
      this.card_number = card_number_;
      this.password = password_;
  }
  
  public String LoginRequest(){
  
      this.request_body_login.append("{username: \"");
      this.request_body_login.append(card_number);
      this.request_body_login.append("\", pass: \"");
      this.request_body_login.append(password);
      this.request_body_login.append("\", lng: \"ua\", prgCode: \"prg1\"}\"");
      Log.i("", "page constructor" + request_body_login.toString());
      return request_body_login.toString();
  }}
  </code></pre>
  
  <p>Login request </p>
  
  <pre><code>    Post https://program.yousystem.com.ua/frontend/api/user/login
  Request {
    "username": "username",
    "pass": "pass",
    "lng": "ua",
    "prgCode": "prg1"
  }
  </code></pre>
  
  <p>Get profile info</p>
  
  <pre><code>    Post https://program.yousystem.com.ua/frontend/api/user/me
  Request {
    "prgCode": "prg1",
    "token": "XT4PHNZBMSK73C7KH33NDJCN8A4SP5CJ"
  }
  </code></pre>
  
  <p>Transaction list</p>
  
  <pre><code>Post https://program.yousystem.com.ua/frontend/api/transaction/list
  Request {
    "withLimit": true,
    "prgCode": "prg1",
    "token": "XT4PHNZBMSK73C7KH33NDJCN8A4SP5CJ"
  }
  </code></pre>