Search in sources :

Example 1 with NetworkFailureException

use of net.micode.notes.gtask.exception.NetworkFailureException 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 2 with NetworkFailureException

use of net.micode.notes.gtask.exception.NetworkFailureException 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)

Aggregations

IOException (java.io.IOException)2 ActionFailureException (net.micode.notes.gtask.exception.ActionFailureException)2 NetworkFailureException (net.micode.notes.gtask.exception.NetworkFailureException)2 HttpResponse (org.apache.http.HttpResponse)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 LinkedList (java.util.LinkedList)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPost (org.apache.http.client.methods.HttpPost)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1