public static String getJSONValue(JSONObject json, String key) { try { if (json.has(key)) return (json.getString(key)); else return "NULL"; } catch (JSONException e) { return "NULL"; } }
/** * Convert a JSONObject into an HTTP header. A request header must contain * * <pre>{ * Method: "POST" (for example), * "Request-URI": "/" (for example), * "HTTP-Version": "HTTP/1.1" (for example) * }</pre> * * A response header must contain * * <pre>{ * "HTTP-Version": "HTTP/1.1" (for example), * "Status-Code": "200" (for example), * "Reason-Phrase": "OK" (for example) * }</pre> * * Any other members of the JSONObject will be output as HTTP fields. The result will end with two * CRLF pairs. * * @param jo A JSONObject * @return An HTTP header string. * @throws JSONException if the object does not contain enough information. */ public static String toString(JSONObject jo) throws JSONException { Iterator keys = jo.keys(); String string; StringBuffer sb = new StringBuffer(); if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { sb.append(jo.getString("HTTP-Version")); sb.append(' '); sb.append(jo.getString("Status-Code")); sb.append(' '); sb.append(jo.getString("Reason-Phrase")); } else if (jo.has("Method") && jo.has("Request-URI")) { sb.append(jo.getString("Method")); sb.append(' '); sb.append('"'); sb.append(jo.getString("Request-URI")); sb.append('"'); sb.append(' '); sb.append(jo.getString("HTTP-Version")); } else { throw new JSONException("Not enough material for an HTTP header."); } sb.append(CRLF); while (keys.hasNext()) { string = keys.next().toString(); if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) && !"Reason-Phrase".equals(string) && !"Method".equals(string) && !"Request-URI".equals(string) && !jo.isNull(string)) { sb.append(string); sb.append(": "); sb.append(jo.getString(string)); sb.append(CRLF); } } sb.append(CRLF); return sb.toString(); }
public void submit_order(String type, String amount, String price) { super.submit_order(type, amount, price); HashMap<String, String> args = new HashMap<String, String>(); args.put("pair", "btc_usd"); if (type.equals("bid")) { args.put("type", "buy"); } else if (type.equals("ask")) { args.put("type", "sell"); } args.put("rate", price); args.put("amount", amount); JSONObject reponse; try { reponse = new JSONObject(query("Trade", args)); if (reponse.has("success") && reponse.getInt("success") == 1) { settings.lasttrade.setOrderid( Integer.toString(reponse.getJSONObject("return").getInt("order_id"))); } } catch (JSONException e) { reponse = new JSONObject(); } utils.logger.log(false, reponse.toString()); needsToUpdateBlances = true; }