git.fiddlerwoaroof.com
Raw Blame History
package com.joinmarrow.marrow.tasks;

import android.os.AsyncTask;
import android.util.Log;

import org.jdeferred.Deferred;

import java.util.HashMap;

import us.monoid.json.JSONObject;
import us.monoid.web.Resty;

/**
 * Created by edwlan on 4/5/15.
 */
public class LogonTask extends AsyncTask<String, Integer, JSONObject> {
    Deferred taskDeferred;
    private String endpoint;

    public LogonTask(Deferred d, String url) {
        super();
        endpoint = url.concat("/api/user/login");
        taskDeferred = d;
    }

    protected JSONObject doInBackground(String... info) {
        String username = info[0];
        String password = info[1];

        JSONObject result = new JSONObject();
        try {
            Log.i("marrow", "getting");

            HashMap<String, String> authInfo = new HashMap<>();
            authInfo.put("username", username);
            authInfo.put("password", password);
            JSONObject postData = new JSONObject(authInfo);
            //Log.i("marrow", postData.toString());

            result = new Resty().json(endpoint, Resty.content(postData)).toObject();
            Log.i("marrow", "gotten");
        } catch (IOException | JSONException e) {
            Log.i("marrow", "fail!");
            Log.e("marrow", e.getLocalizedMessage());
        }
        return result;
    }

    protected void onPostExecute(JSONObject result) {
        boolean success = false;
        try {
            success = result.getBoolean("status");
            taskDeferred.resolve(success);
        } catch (JSONException e) {
            e.printStackTrace();
            taskDeferred.reject(result);
        }
    }
}