package com.joinmarrow.marrow.tasks;
import android.os.AsyncTask;
import android.util.Log;
import org.jdeferred.Deferred;
import java.io.IOException;
import java.util.HashMap;
import us.monoid.json.JSONArray;
import us.monoid.json.JSONException;
import us.monoid.json.JSONObject;
import us.monoid.web.Resty;
/**
* Created by edwlan on 4/6/15.
*/
public class AddLink extends AsyncTask<String, Void, JSONObject> {
Deferred<JSONObject, JSONObject, Void> taskDeferred;
private String endpoint;
public AddLink(Deferred d, String url) {
super();
endpoint = url.concat("/api/bones/add");
taskDeferred = d;
}
protected JSONObject doInBackground(String... link) {
String linkToAdd = link[0],
title = "";
JSONObject result = new JSONObject();
try {
Log.i("marrow", "getting");
HashMap<String, String> linkInfo = new HashMap<>();
linkInfo.put("url", linkToAdd);
linkInfo.put("title", title);
JSONObject postData = new JSONObject(linkInfo);
//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) {
taskDeferred.resolve(result);
}
}