Search in sources :

Example 1 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskClient method moveTask.

public void moveTask(Task task, TaskList preParent, TaskList curParent) throws NetworkFailureException {
    commitUpdate();
    try {
        JSONObject jsPost = new JSONObject();
        JSONArray actionList = new JSONArray();
        JSONObject action = new JSONObject();
        // action_list
        action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
        action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
        action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
        if (preParent == curParent && task.getPriorSibling() != null) {
            // put prioring_sibing_id only if moving within the tasklist and
            // it is not the first one
            action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
        }
        action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid());
        action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid());
        if (preParent != curParent) {
            // put the dest_list only if moving between tasklists
            action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
        }
        actionList.put(action);
        jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
        // client_version
        jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
        postRequest(jsPost);
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("move task: handing jsonobject failed");
    }
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 2 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskClient method getTaskLists.

public JSONArray getTaskLists() throws NetworkFailureException {
    if (!mLoggedin) {
        Log.e(TAG, "please login first");
        throw new ActionFailureException("not logged in");
    }
    try {
        HttpGet httpGet = new HttpGet(mGetUrl);
        HttpResponse response = null;
        response = mHttpClient.execute(httpGet);
        // get the task list
        String resString = getResponseContent(response.getEntity());
        String jsBegin = "_setup(";
        String jsEnd = ")}</script>";
        int begin = resString.indexOf(jsBegin);
        int end = resString.lastIndexOf(jsEnd);
        String jsString = null;
        if (begin != -1 && end != -1 && begin < end) {
            jsString = resString.substring(begin + jsBegin.length(), end);
        }
        JSONObject js = new JSONObject(jsString);
        return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("gettasklists: httpget failed");
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("gettasklists: httpget failed");
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("get task lists: handing jasonobject failed");
    }
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) JSONObject(org.json.JSONObject) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) NetworkFailureException(net.micode.notes.gtask.exception.NetworkFailureException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 3 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskClient method postRequest.

private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
    if (!mLoggedin) {
        Log.e(TAG, "please login first");
        throw new ActionFailureException("not logged in");
    }
    HttpPost httpPost = createHttpPost();
    try {
        LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
        list.add(new BasicNameValuePair("r", js.toString()));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entity);
        // execute the post
        HttpResponse response = mHttpClient.execute(httpPost);
        String jsString = getResponseContent(response.getEntity());
        return new JSONObject(jsString);
    } catch (ClientProtocolException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("postRequest failed");
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new NetworkFailureException("postRequest failed");
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("unable to convert response content to jsonobject");
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("error occurs when posting request");
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONException(org.json.JSONException) NetworkFailureException(net.micode.notes.gtask.exception.NetworkFailureException) IOException(java.io.IOException) ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NetworkFailureException(net.micode.notes.gtask.exception.NetworkFailureException)

Example 4 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskClient method getTaskList.

public JSONArray getTaskList(String listGid) throws NetworkFailureException {
    commitUpdate();
    try {
        JSONObject jsPost = new JSONObject();
        JSONArray actionList = new JSONArray();
        JSONObject action = new JSONObject();
        // action_list
        action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
        action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
        action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid);
        action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
        actionList.put(action);
        jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
        // client_version
        jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
        JSONObject jsResponse = postRequest(jsPost);
        return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("get task list: handing jsonobject failed");
    }
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 5 with ActionFailureException

use of net.micode.notes.gtask.exception.ActionFailureException in project Notes by MiCode.

the class GTaskClient method createTask.

public void createTask(Task task) throws NetworkFailureException {
    commitUpdate();
    try {
        JSONObject jsPost = new JSONObject();
        JSONArray actionList = new JSONArray();
        // action_list
        actionList.put(task.getCreateAction(getActionId()));
        jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
        // client_version
        jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
        // post
        JSONObject jsResponse = postRequest(jsPost);
        JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
        task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("create task: handing jsonobject failed");
    }
}
Also used : ActionFailureException(net.micode.notes.gtask.exception.ActionFailureException) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Aggregations

ActionFailureException (net.micode.notes.gtask.exception.ActionFailureException)22 JSONException (org.json.JSONException)14 JSONObject (org.json.JSONObject)14 JSONArray (org.json.JSONArray)7 Task (net.micode.notes.gtask.data.Task)5 SqlNote (net.micode.notes.gtask.data.SqlNote)4 TaskList (net.micode.notes.gtask.data.TaskList)4 Node (net.micode.notes.gtask.data.Node)3 Cursor (android.database.Cursor)2 Uri (android.net.Uri)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MetaData (net.micode.notes.gtask.data.MetaData)2 NetworkFailureException (net.micode.notes.gtask.exception.NetworkFailureException)2 HttpResponse (org.apache.http.HttpResponse)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 ContentValues (android.content.ContentValues)1 LinkedList (java.util.LinkedList)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1