Example usage for org.json JSONException printStackTrace

List of usage examples for org.json JSONException printStackTrace

Introduction

In this page you can find the example usage for org.json JSONException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:com.citrus.mobile.RESTclient.java

public JSONObject makeSendMoneyRequest(String accessToken, CitrusUser toUser, Amount amount, String message) {
    HttpsURLConnection conn;//from  ww  w .java2 s. co m
    DataOutputStream wr = null;
    JSONObject txnDetails = null;
    BufferedReader in = null;

    try {
        String url = null;

        StringBuffer postDataBuff = new StringBuffer("amount=");
        postDataBuff.append(amount.getValue());

        postDataBuff.append("&currency=");
        postDataBuff.append(amount.getCurrency());

        postDataBuff.append("&message=");
        postDataBuff.append(message);

        if (!TextUtils.isEmpty(toUser.getEmailId())) {
            url = urls.getString(base_url) + urls.getString("transfer");
            postDataBuff.append("&to=");
            postDataBuff.append(toUser.getEmailId());

        } else if (!TextUtils.isEmpty(toUser.getMobileNo())) {
            url = urls.getString(base_url) + urls.getString("suspensetransfer");

            postDataBuff.append("&to=");
            postDataBuff.append(toUser.getMobileNo());

        }

        conn = (HttpsURLConnection) new URL(url).openConnection();

        //add reuqest header
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Authorization", "Bearer " + accessToken);

        conn.setDoOutput(true);
        wr = new DataOutputStream(conn.getOutputStream());

        wr.writeBytes(postDataBuff.toString());
        wr.flush();

        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + postDataBuff.toString());
        System.out.println("Response Code : " + responseCode);

        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        txnDetails = new JSONObject(response.toString());

    } catch (JSONException exception) {
        exception.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            if (wr != null) {
                wr.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return txnDetails;
}

From source file:com.citrus.mobile.RESTclient.java

public JSONObject makePostrequest() throws IOException {
    HttpParams redirectparams = new BasicHttpParams();
    redirectparams.setParameter("http.protocol.handle-redirects", false);

    httpClient = new DefaultHttpClient();
    HttpPost httpPost = null;// ww w. j av  a 2 s  .c o m
    try {
        httpPost = new HttpPost(urls.getString(base_url) + urls.getString(type));
        httpPost.setParams(redirectparams);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    if (params != null) {
        List<NameValuePair> postData = new ArrayList<NameValuePair>(2);
        Iterator<String> iter = params.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            try {
                String value = params.getString(key);
                postData.add(new BasicNameValuePair(key, value));
            } catch (JSONException e) {
                Log.d("exception", e.toString());
            }
        }

        httpPost.setEntity(new UrlEncodedFormEntity(postData));
    }
    Iterator<String> iterhead = headers.keys();
    while (iterhead.hasNext()) {
        String key = iterhead.next();
        try {
            String value = headers.getString(key);
            httpPost.addHeader(key, value);
        } catch (JSONException e) {
            return null;
        }
    }

    try {
        response = httpClient.execute(httpPost);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return parseResponse(response);
}

From source file:com.citrus.mobile.RESTclient.java

public JSONObject makePutrequest() {
    HttpClient client = new DefaultHttpClient();

    HttpPut put = null;// w  w  w  .ja v a  2 s.c  o m
    try {
        put = new HttpPut(urls.getString(base_url) + urls.getString(type));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    Iterator<String> iterhead = headers.keys();
    while (iterhead.hasNext()) {
        String key = iterhead.next();
        try {
            String value = headers.getString(key);
            put.addHeader(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    List<NameValuePair> putdata = new ArrayList<NameValuePair>(2);
    Iterator<String> iter = params.keys();
    while (iter.hasNext()) {
        String key = iter.next();
        try {
            String value = params.getString(key);
            putdata.add(new BasicNameValuePair(key, value));
        } catch (JSONException e) {
            Log.d("exception", e.toString());
        }
    }

    try {
        put.setEntity(new UrlEncodedFormEntity(putdata));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    try {
        response = client.execute(put);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return parseResponse(response);
}

From source file:com.citrus.mobile.RESTclient.java

public JSONObject makePutrequest(JSONObject details) {
    HttpClient client = new DefaultHttpClient();

    HttpPut put = null;/*from  w w w .  j  a  v a2  s  .c  o  m*/
    try {
        put = new HttpPut(urls.getString(base_url) + urls.getString(type));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    Iterator<String> iterhead = headers.keys();
    while (iterhead.hasNext()) {
        String key = iterhead.next();
        try {
            String value = headers.getString(key);
            put.addHeader(key, value);
        } catch (JSONException e) {
            Log.d("exception", e.toString());
        }
    }

    try {
        put.setEntity(new StringEntity(details.toString()));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    try {
        response = client.execute(put);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return parseResponse(response);
}

From source file:com.citrus.mobile.RESTclient.java

public JSONObject makegetRequest() throws JSONException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = null;/*from w w w . j a  va  2s.  c  o m*/
    try {
        httpGet = new HttpGet(urls.getString(base_url) + urls.getString(type));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    Iterator<String> iterhead = headers.keys();
    while (iterhead.hasNext()) {
        String key = iterhead.next();
        try {
            String value = headers.getString(key);
            httpGet.addHeader(key, value);
        } catch (JSONException e) {
            return new JSONObject().put("error", "unable to find headers");
        }
    }

    try {
        response = httpClient.execute(httpGet);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return parseResponse(response);

}

From source file:com.citrus.mobile.RESTclient.java

public JSONObject postPayment(JSONObject payment) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = null;/* ww w .  j  av a2s .  c om*/
    try {
        httpPost = new HttpPost(urls.getString(base_url) + urls.getString(type));
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }

    httpPost.setHeader("Content-Type", "application/json");

    try {
        httpPost.setEntity(new StringEntity(payment.toString()));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }

    try {
        response = httpClient.execute(httpPost);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return parseResponse(response);
}

From source file:com.citrus.mobile.RESTclient.java

private JSONObject parseResponse(HttpResponse response) {
    try {/*from  ww w  . j a v  a  2s  . com*/

        if (response == null) {
            return formError(600, "Null response - is your internet connection functional?");
        }

        switch (response.getStatusLine().getStatusCode()) {
        case HttpStatus.SC_OK:
            return new JSONObject(EntityUtils.toString(response.getEntity()));
        case HttpStatus.SC_NO_CONTENT:
            return null;
        case HttpStatus.SC_BAD_REQUEST:
            return formError(400, "badrequest");
        case HttpStatus.SC_UNAUTHORIZED:
            return formError(401, "unauthorized");
        case HttpStatus.SC_FORBIDDEN:
            return formError(403, "access forbidden");
        case HttpStatus.SC_SERVICE_UNAVAILABLE:
            return formError(503, "unavailable");
        case HttpStatus.SC_GATEWAY_TIMEOUT:
            return formError(504, "gatewaytimeout");
        case HttpStatus.SC_MOVED_TEMPORARILY:
            return getCookies(response);

        default:
            return formError(response.getStatusLine().getStatusCode(), "unknownerror");
        }
    } catch (JSONException e) {
        e.printStackTrace();
        return formError(600, "jsonexception");
    } catch (IOException e) {
        e.printStackTrace();
        return formError(600, "ioexception");
    }

}

From source file:com.citrus.mobile.RESTclient.java

private JSONObject getCookies(HttpResponse response) {
    Header[] headers = response.getAllHeaders();
    JSONObject cookies = new JSONObject();
    for (int i = 0; i < headers.length; i++) {
        Header h = headers[i];//from w w w  .  ja  va2  s.  c o m
        try {
            cookies.put(h.getName(), h.getValue());
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
    return cookies;
}

From source file:com.citrus.mobile.RESTclient.java

private JSONObject formError(int status, String message) {
    JSONObject error = new JSONObject();
    try {/* w ww .j  a v  a 2s  .  c om*/
        error.put("error", String.valueOf(status));
        error.put("message", message);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return error;
}

From source file:com.asd.littleprincesbeauty.data.TaskList.java

@Override
public JSONObject getCreateAction(int actionId) {
    JSONObject js = new JSONObject();

    try {//from   w  w  w .j a v a2s  . c om
        // action_type
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);

        // action_id
        js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);

        // index
        js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex);

        // entity_delta
        JSONObject entity = new JSONObject();
        entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
        entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
        entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
        js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);

    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        throw new ActionFailureException("fail to generate tasklist-create jsonobject");
    }

    return js;
}