teja2495
10/20/2018 - 4:50 AM

postData Asynchronous (okHttp)

void postData(String url, String email, String password) {
    OkHttpClient client = new OkHttpClient();
    RequestBody formBody = new FormBody.Builder()
        .add("email", email)
        .add("password", password)
        .build();
    Request request = new Request.Builder()
        .url(url)
        .post(formBody)
        .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try {
                JSONObject json = new JSONObject(response.body().string());
                if (json.getString("status").equals("ok")) {
                    setPreferences("token", json.getString("token"), MainActivity.this);
                    backgroundThreadShortToast(MainActivity.this, "Login Successful");
                    username = json.getString("user_fname") + " " + json.getString("user_lname");
                    user_id = json.get("user_id").toString();
                } else {
                    backgroundThreadShortToast(MainActivity.this, json.getString("message"));
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
}