Example usage for org.json JSONObject getJSONObject

List of usage examples for org.json JSONObject getJSONObject

Introduction

In this page you can find the example usage for org.json JSONObject getJSONObject.

Prototype

public JSONObject getJSONObject(String key) throws JSONException 

Source Link

Document

Get the JSONObject value associated with a key.

Usage

From source file:com.jvoid.quote.controller.JVoidQuoteController.java

@RequestMapping(value = "quote/addaddress", method = RequestMethod.GET)
public @ResponseBody String addAddress(@RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {

    int cartId = -1;
    JSONObject user = null;//from www  . j a  v  a2s  . c  o m
    JSONObject billing = null;
    JSONObject shipping = null;
    String checkoutMethod = null;
    try {
        cartId = jsonParams.getInt("cartId");
        user = jsonParams.getJSONObject("user");
        billing = jsonParams.getJSONObject("billing");
        shipping = jsonParams.getJSONObject("shipping");
        checkoutMethod = jsonParams.getString("checkoutMethod");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    CheckoutQuote checkoutQuote = this.checkoutQuoteService.getCheckoutQuoteById(cartId);
    checkoutQuote.setCheckoutMethod(checkoutMethod);
    checkoutQuote.setUpdatedAt(Utilities.getCurrentDateTime());
    this.checkoutQuoteService.updateCheckoutQuote(checkoutQuote);

    try {
        CheckoutQuoteAddress checkoutQuoteAddress = new CheckoutQuoteAddress();
        checkoutQuoteAddress.setQuoteId(cartId);

        checkoutQuoteAddress.setFirstname(user.getString("firstName"));
        checkoutQuoteAddress.setLastname(user.getString("lastName"));
        checkoutQuoteAddress.setCompany(user.getString("company"));
        checkoutQuoteAddress.setEmail(user.getString("emailAddress"));

        checkoutQuoteAddress.setAddressType("billing");
        checkoutQuoteAddress
                .setStreet(billing.getString("address") + "," + billing.getString("streetAddress2"));
        checkoutQuoteAddress.setCity(billing.getString("city") + "," + billing.getString("state"));
        checkoutQuoteAddress.setCountryId(billing.getString("country"));
        checkoutQuoteAddress.setPostcode(billing.getString("zip"));
        checkoutQuoteAddress.setTelephone(billing.getString("telephone"));
        checkoutQuoteAddress.setFax(billing.getString("fax"));

        checkoutQuoteAddress.setCreatedAt(Utilities.getCurrentDateTime());
        checkoutQuoteAddress.setUpdatedAt(Utilities.getCurrentDateTime());

        this.checkoutQuoteAddressService.addCheckoutQuoteAddress(checkoutQuoteAddress);

        checkoutQuoteAddress = null;
        checkoutQuoteAddress = new CheckoutQuoteAddress();
        checkoutQuoteAddress.setQuoteId(cartId);

        checkoutQuoteAddress.setFirstname(user.getString("firstName"));
        checkoutQuoteAddress.setLastname(user.getString("lastName"));
        checkoutQuoteAddress.setCompany(user.getString("company"));
        checkoutQuoteAddress.setEmail(user.getString("emailAddress"));

        checkoutQuoteAddress.setAddressType("shipping");
        checkoutQuoteAddress
                .setStreet(shipping.getString("address") + "," + shipping.getString("streetAddress2"));
        checkoutQuoteAddress.setCity(shipping.getString("city") + "," + shipping.getString("state"));
        checkoutQuoteAddress.setCountryId(shipping.getString("country"));
        checkoutQuoteAddress.setPostcode(shipping.getString("zip"));
        checkoutQuoteAddress.setTelephone(shipping.getString("telephone"));
        checkoutQuoteAddress.setFax(shipping.getString("fax"));

        checkoutQuoteAddress.setCreatedAt(Utilities.getCurrentDateTime());
        checkoutQuoteAddress.setUpdatedAt(Utilities.getCurrentDateTime());

        this.checkoutQuoteAddressService.addCheckoutQuoteAddress(checkoutQuoteAddress);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONObject jsonObj = new JSONObject();
    try {
        jsonObj.put("cartId", cartId);
        jsonObj.put("result", "Success");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonObj.toString();
}

From source file:org.achtern.AchternEngine.core.resource.loader.json.JsonLoader.java

/**
 * Reads a {@link org.achtern.AchternEngine.core.rendering.texture.Texture} from a JSON
 * declaration.//from w  w  w  .  j  av  a 2  s  .c  o  m
 * Loads it via the {@link org.achtern.AchternEngine.core.resource.ResourceLoader}
 * @param json JSONObject
 * @return Texture
 * @throws Exception (from ResourceLoader)
 */
protected Texture getTexture(JSONObject json) throws Exception {

    if (json.has("file")) {
        return ResourceLoader.getTexture(json.getString("file"));
    }

    // Search for a plain color
    if (json.has("solid")) {
        Color c = getColor(json.getJSONObject("solid"));

        return new Texture(ImageGenerator.bytesFromColor(c));
    }

    throw new ParsingException("Texture declaration illegal, missing <file> or <solid> key");

}

From source file:net.cellcloud.talk.HttpDialogueHandler.java

@Override
protected void doPost(HttpRequest request, HttpResponse response) throws IOException {
    HttpSession session = request.getSession();
    if (null != session) {
        try {/*from  www  .ja  v a2  s  .  c  o m*/
            // ??
            JSONObject json = new JSONObject(new String(request.readRequestData(), Charset.forName("UTF-8")));
            // ? JSON ?
            String speakerTag = json.getString(Tag);
            String celletIdentifier = json.getString(Identifier);
            JSONObject primitiveJSON = json.getJSONObject(Primitive);
            // ?
            Primitive primitive = new Primitive(speakerTag);
            PrimitiveSerializer.read(primitive, primitiveJSON);
            // ?
            this.talkService.processDialogue(session, speakerTag, celletIdentifier, primitive);

            // ?
            // FIXME 2014/10/03 ??
            JSONObject responseData = new JSONObject();

            // ??
            Queue<Message> queue = session.getQueue();
            if (!queue.isEmpty()) {
                ArrayList<String> identifiers = new ArrayList<String>(queue.size());
                ArrayList<Primitive> primitives = new ArrayList<Primitive>(queue.size());
                for (int i = 0, size = queue.size(); i < size; ++i) {
                    // ?
                    Message message = queue.poll();
                    // 
                    Packet packet = Packet.unpack(message.get());
                    if (null != packet) {
                        // ? cellet identifier
                        byte[] identifier = packet.getSubsegment(1);

                        // ?????
                        byte[] primData = packet.getSubsegment(0);
                        ByteArrayInputStream stream = new ByteArrayInputStream(primData);

                        // ???
                        Primitive prim = new Primitive(Nucleus.getInstance().getTagAsString());
                        prim.read(stream);

                        // 
                        identifiers.add(Utils.bytes2String(identifier));
                        primitives.add(prim);
                    }
                }

                // ?
                JSONArray jsonPrimitives = this.convert(identifiers, primitives);
                responseData.put(Primitives, jsonPrimitives);
            }

            // ?
            responseData.put(Queue, queue.size());

            // ?
            this.respondWithOk(response, responseData);
        } catch (JSONException e) {
            Logger.log(HttpDialogueHandler.class, e, LogLevel.ERROR);
            this.respond(response, HttpResponse.SC_BAD_REQUEST);
        }
    } else {
        this.respond(response, HttpResponse.SC_UNAUTHORIZED);
    }
}

From source file:com.cssweb.android.view.KlineView.java

/**
 * ??//  w  w  w  . j a  v  a 2  s .  co  m
 * @throws JSONException 
 */
private void makeTodayData() throws JSONException {
    int l = quoteData.getJSONArray("K").length();
    if (quoteData.isNull("joTMP")) {//temp??
        newStockhandler();
        return;
    }
    if (!quoteData.getBoolean("tradeFlag")) {
        //??????
        //???0??
        return;
    }
    JSONObject tempvalue = quoteData.getJSONObject("joTMP");
    double zrsp = quoteData.getDouble("zrsp");
    period = quoteData.getString("period");
    if (period.equals("week") || period.equals("month") || period.equals("year")) {
        if (tempvalue.getString(period) != null) {
            if (tempvalue.isNull("ma") || quoteData.isNull("MA") || quoteData.isNull("K")) {
                makeTmpData();
                return;
            }
            int tp = quoteData.getInt("tp");
            if (tp == 1) {
                String date = tempvalue.getJSONObject(period).getString("date");
                if (DateTool.isSameWeekMonthYear(date, period)) {
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(1,
                            tempvalue.getJSONObject(period).getDouble("open"));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(2,
                            tempvalue.getJSONObject(period).getDouble("high"));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(3,
                            tempvalue.getJSONObject(period).getDouble("low"));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(4, zrsp);
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(5,
                            tempvalue.getJSONObject(period).getDouble("cjsl"));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(6,
                            tempvalue.getJSONObject(period).getDouble("cjje"));
                    //quoteData.getJSONArray("K").getJSONArray(l-1).put(0, ) ;
                    zrsp = tempvalue.getJSONObject(period).getDouble("close");
                    Log.i("#####period->zrsp####",
                            quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(4) + ">>>>>>>>" + zrsp);
                }
            } else {
                int spayday = quoteData.getInt("spday");
                double cjsl = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(5);
                double cjje = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(6);
                if (spayday == 0) {//0??temp?????
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(2,
                            Math.max(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(2),
                                    tempvalue.getJSONObject(period).getDouble("high")));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(3,
                            Math.min(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(3),
                                    tempvalue.getJSONObject(period).getDouble("low")));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(5,
                            cjsl + tempvalue.getJSONObject(period).getDouble("cjsl"));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(6,
                            cjje + tempvalue.getJSONObject(period).getDouble("cjje"));
                } else {
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(2,
                            Math.max(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(2),
                                    tempvalue.getJSONObject(period).getDouble("high")));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(3,
                            Math.min(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(3),
                                    tempvalue.getJSONObject(period).getDouble("low")));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(5,
                            tempvalue.getJSONObject(period).getDouble("cjsl"));
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(6,
                            tempvalue.getJSONObject(period).getDouble("cjje"));
                }
                double jrkp = tempvalue.getJSONObject(period).getDouble("open");
                if (jrkp != 0) {
                    quoteData.getJSONArray("K").getJSONArray(l - 1).put(1, jrkp);
                }
                zrsp = tempvalue.getJSONObject(period).getDouble("close");
            }
        }
    }

    String qt = quoteData.getJSONArray("K").getJSONArray(l - 1).getString(0);
    double high = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(2);
    double low = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(3);
    double zjcj = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(4);
    double cjsl = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(5);

    double summa4 = tempvalue.getJSONObject("ma").getDouble("sumMa4");
    double summa9 = tempvalue.getJSONObject("ma").getDouble("sumMa9");
    double summa19 = tempvalue.getJSONObject("ma").getDouble("sumMa19");
    double summa59 = tempvalue.getJSONObject("ma").getDouble("sumMa59");
    double sumvolma4 = tempvalue.getJSONObject("ma").getDouble("sumMavol4");
    double sumvolma9 = tempvalue.getJSONObject("ma").getDouble("sumMavol9");

    if (mainIndicatorType.toUpperCase().equals("MA") || mainIndicatorType.toUpperCase().equals("BOLL")) {
        quoteData.getJSONArray("MA").put(new JSONArray());
        quoteData.getJSONArray("MA").getJSONArray(l - 1).put(0, qt);
        double ma5 = 0;
        if (l > 4)
            ma5 = (summa4 + zjcj) / 5;
        quoteData.getJSONArray("MA").getJSONArray(l - 1).put(1, ma5);
        double ma10 = 0;
        if (l > 9)
            ma10 = (summa9 + zjcj) / 10;
        quoteData.getJSONArray("MA").getJSONArray(l - 1).put(2, ma10);
        double ma20 = 0;
        if (l > 19)
            ma20 = (summa19 + zjcj) / 20;
        quoteData.getJSONArray("MA").getJSONArray(l - 1).put(3, ma20);
        double ma60 = 0;
        if (l > 59)
            ma60 = (summa59 + zjcj) / 60;
        quoteData.getJSONArray("MA").getJSONArray(l - 1).put(4, ma60);
    }
    double mavol5 = (sumvolma4 + cjsl) / 5;
    quoteData.getJSONArray("MA").getJSONArray(l - 1).put(5, mavol5);

    double mavol10 = (sumvolma9 + cjsl) / 10;
    quoteData.getJSONArray("MA").getJSONArray(l - 1).put(6, mavol10);

    if (mainIndicatorType.equals("BOLL")) {
        quoteData.getJSONArray("BOLL").put(new JSONArray());
        double mid = 0;
        double upper = 0;
        double lower = 0;
        if (l > 25) {
            double sumClose = tempvalue.getJSONObject("boll").getDouble("sumClose");
            double sumPowClose = tempvalue.getJSONObject("boll").getDouble("sumPowClose");
            double maPow = 0;
            double temp;
            if (l > 25) {
                mid = (sumClose + zjcj) / 26;
                maPow = (sumPowClose + zjcj * zjcj) / 26;
                temp = (maPow - mid * mid);
                if (temp < 0)
                    temp = 0;
                upper = mid + 2 * Math.sqrt((temp * 26) / (26 - 1));
                lower = mid - 2 * Math.sqrt((temp * 26) / (26 - 1));
            }
        }

        quoteData.getJSONArray("BOLL").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("BOLL").getJSONArray(l - 1).put(1, mid);
        quoteData.getJSONArray("BOLL").getJSONArray(l - 1).put(2, upper);
        quoteData.getJSONArray("BOLL").getJSONArray(l - 1).put(3, lower);
    }

    if (indicatorType.equals("MACD")) {
        quoteData.getJSONArray("MACD").put(new JSONArray());
        double prevemashort = 0;
        double prevemalong = 0;
        double prevdea = 0;
        double dif = 0;
        double dea = 0;
        double macd = 0;
        double emashort = 0;
        double emalong = 0;

        prevemashort = tempvalue.getJSONObject("macd").getDouble("emaShort");
        prevemalong = tempvalue.getJSONObject("macd").getDouble("emaLong");
        prevdea = tempvalue.getJSONObject("macd").getDouble("dea");

        if (l > 1) {
            emashort = (2 * zjcj + (12 - 1) * prevemashort) / (12 + 1);
            emalong = (2 * zjcj + (26 - 1) * prevemalong) / (26 + 1);
            dif = emashort - emalong;
            dea = (2 * dif + (9 - 1) * prevdea) / (9 + 1);
            macd = (dif - dea) * 2;
        }

        quoteData.getJSONArray("MACD").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("MACD").getJSONArray(l - 1).put(1, dif);
        quoteData.getJSONArray("MACD").getJSONArray(l - 1).put(2, dea);
        quoteData.getJSONArray("MACD").getJSONArray(l - 1).put(3, macd);
    }
    if (indicatorType.equals("BIAS")) {
        quoteData.getJSONArray("BIAS").put(new JSONArray());
        double sum5 = tempvalue.getJSONObject("bias").getDouble("sum5");
        double sum11 = tempvalue.getJSONObject("bias").getDouble("sum11");
        double sum23 = tempvalue.getJSONObject("bias").getDouble("sum23");

        double bias1 = l >= 6 ? (zjcj - (sum5 + zjcj) / 6) / ((sum5 + zjcj) / 6) * 100 : 0;
        double bias2 = l >= 12 ? (zjcj - (sum11 + zjcj) / 12) / ((sum11 + zjcj) / 12) * 100 : 0;
        double bias3 = l >= 24 ? (zjcj - (sum23 + zjcj) / 24) / ((sum23 + zjcj) / 24) * 100 : 0;
        quoteData.getJSONArray("BIAS").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("BIAS").getJSONArray(l - 1).put(1, bias1);
        quoteData.getJSONArray("BIAS").getJSONArray(l - 1).put(2, bias2);
        quoteData.getJSONArray("BIAS").getJSONArray(l - 1).put(3, bias3);
    }
    if (indicatorType.equals("RSI")) {
        quoteData.getJSONArray("RSI").put(new JSONArray());

        double rsi1 = 0;
        double rsi2 = 0;
        double rsi3 = 0;

        if (l > 1 && tempvalue.has("rsi")) {
            double smaMax1 = tempvalue.getJSONObject("rsi").getDouble("smaMax1");
            double smaMax2 = tempvalue.getJSONObject("rsi").getDouble("smaMax2");
            double smaMax3 = tempvalue.getJSONObject("rsi").getDouble("smaMax3");
            double smaAbs1 = tempvalue.getJSONObject("rsi").getDouble("smaAbs1");
            double smaAbs2 = tempvalue.getJSONObject("rsi").getDouble("smaAbs2");
            double smaAbs3 = tempvalue.getJSONObject("rsi").getDouble("smaAbs3");

            double rsiMax1 = (Math.max(zjcj - zrsp, 0.0) * 1 + smaMax1 * (6 - 1)) / 6;
            double rsiAbs1 = (Math.abs(zjcj - zrsp) * 1 + smaAbs1 * (6 - 1)) / 6;
            if (rsiAbs1 == 0)
                rsi1 = 0;
            else
                rsi1 = rsiMax1 / rsiAbs1 * 100;

            double rsiMax2 = (Math.max(zjcj - zrsp, 0.0) * 1 + smaMax2 * (12 - 1)) / 12;
            double rsiAbs2 = (Math.abs(zjcj - zrsp) * 1 + smaAbs2 * (12 - 1)) / 12;
            if (rsiAbs2 == 0)
                rsi2 = 0;
            else
                rsi2 = rsiMax2 / rsiAbs2 * 100;

            double rsiMax3 = (Math.max(zjcj - zrsp, 0.0) * 1 + smaMax3 * (24 - 1)) / 24;
            double rsiAbs3 = (Math.abs(zjcj - zrsp) * 1 + smaAbs3 * (24 - 1)) / 24;
            if (rsiAbs3 == 0)
                rsi3 = 0;
            else
                rsi3 = rsiMax3 / rsiAbs3 * 100;
        }
        quoteData.getJSONArray("RSI").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("RSI").getJSONArray(l - 1).put(1, rsi1);
        quoteData.getJSONArray("RSI").getJSONArray(l - 1).put(2, rsi2);
        quoteData.getJSONArray("RSI").getJSONArray(l - 1).put(3, rsi3);
    }
    if (indicatorType.equals("KDJ")) {
        quoteData.getJSONArray("KDJ").put(new JSONArray());
        double newk = 0;
        double newd = 0;
        double newj = 0;

        if (l > 1 && tempvalue.has("kdj")) {
            double K = quoteData.getJSONArray("KDJ").getJSONArray(l - 2).getDouble(1);
            double D = quoteData.getJSONArray("KDJ").getJSONArray(l - 2).getDouble(2);
            double HHV = tempvalue.getJSONObject("kdj").getDouble("hhv");
            double LLV = tempvalue.getJSONObject("kdj").getDouble("llv");
            double nowllv = 0.0;

            if (LLV < low) // l?
                nowllv = LLV;
            else
                nowllv = low;

            double nowhhv = 0.0;
            if (HHV > high)
                nowhhv = HHV;
            else
                nowhhv = high;
            double rsv;
            if (Math.abs(nowhhv - nowllv) < 0.0001) {
                rsv = 0;
            } else {
                rsv = (zjcj - nowllv) / (nowhhv - nowllv) * 100;
            }
            newk = (rsv * 1 + K * (3 - 1)) / 3;
            newd = (newk * 1 + D * (3 - 1)) / 3;
            newj = 3 * newk - 2 * newd;
        }
        quoteData.getJSONArray("KDJ").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("KDJ").getJSONArray(l - 1).put(1, newk);
        quoteData.getJSONArray("KDJ").getJSONArray(l - 1).put(2, newd);
        quoteData.getJSONArray("KDJ").getJSONArray(l - 1).put(3, newj);
    }
    if (indicatorType.equals("CCI")) {
        double CCI = 0;
        quoteData.getJSONArray("CCI").put(new JSONArray());
        if (l > 13 && tempvalue.has("cci")) {
            JSONArray typlist = tempvalue.getJSONObject("cci").getJSONArray("typ");
            double sumTyp = 0;
            double TYP = (zjcj + high + low) / 3;
            double rit = 0;
            for (int i = 0; i < typlist.length(); i++) {
                rit = typlist.getDouble(i);
                if (i == 13)
                    break;
                sumTyp += rit;
            }
            sumTyp += TYP;
            double ma = sumTyp / 14;

            sumTyp = 0;
            for (int i = 0; i < typlist.length(); i++) {
                rit = typlist.getDouble(i);
                if (i == 13)
                    break;
                sumTyp += Math.abs(rit - ma);
            }
            sumTyp += Math.abs(TYP - ma);
            double avedev = sumTyp / 14;

            if (avedev == 0)
                CCI = 0;
            else
                CCI = (TYP - ma) / (0.015 * avedev);
        }
        quoteData.getJSONArray("CCI").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("CCI").getJSONArray(l - 1).put(1, CCI);
    }
    if (indicatorType.equals("OBV")) {
        quoteData.getJSONArray("OBV").put(new JSONArray());
        double obv = 0, maobv = 0;
        if (tempvalue.has("obv")) {
            if (zjcj > zrsp) {
                obv = tempvalue.getJSONObject("obv").getDouble("obv") + cjsl;
            }
            if (zjcj == zrsp) {
                obv = 0;
            }
            if (zjcj < zrsp) {
                obv = tempvalue.getJSONObject("obv").getDouble("obv") - cjsl;
            }
            if (l >= 29) {
                maobv = (tempvalue.getJSONObject("obv").getDouble("sumObv29") + obv) / 30;
            } else {
                maobv = 0;
            }
        }
        quoteData.getJSONArray("OBV").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("OBV").getJSONArray(l - 1).put(1, obv);
        quoteData.getJSONArray("OBV").getJSONArray(l - 1).put(2, maobv);
    }
    if (indicatorType.equals("PSY")) {
        quoteData.getJSONArray("PSY").put(new JSONArray());
        double countPsy = 0, psy = 0, psyma = 0;
        if (zjcj > zrsp) {
            countPsy = 1;
        } else {
            countPsy = 0;
        }
        if (l >= 11 && tempvalue.has("psy")) {
            countPsy += tempvalue.getJSONObject("psy").getDouble("psyCount11");
            psy = countPsy / 12 * 100;
            psyma = (tempvalue.getJSONObject("psy").getDouble("sumPsy") + psy) / 6;
        } else {
            psy = 0;
        }
        quoteData.getJSONArray("PSY").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("PSY").getJSONArray(l - 1).put(1, psy);
        quoteData.getJSONArray("PSY").getJSONArray(l - 1).put(2, psyma);
    }
    if (indicatorType.equals("ROC")) {
        quoteData.getJSONArray("ROC").put(new JSONArray());
        double roc = 0, rocma = 0;
        if (l > 12 && tempvalue.has("roc")) {
            double refClose12 = tempvalue.getJSONObject("roc").getDouble("refClose12");
            if (refClose12 == 0) {
                roc = 0;
            } else {
                roc = 100 * (zjcj - refClose12) / refClose12;
            }
            rocma = (tempvalue.getJSONObject("roc").getDouble("sumRoc") + roc) / 6;
        } else {
            roc = 0;
        }
        quoteData.getJSONArray("ROC").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("ROC").getJSONArray(l - 1).put(1, roc);
        quoteData.getJSONArray("ROC").getJSONArray(l - 1).put(2, rocma);
    }
    if (indicatorType.equals("WR")) {
        quoteData.getJSONArray("WR").put(new JSONArray());
        double wr = 0, wr2 = 0;
        if (l > 9 && tempvalue.has("wr")) {
            double llv = tempvalue.getJSONObject("wr").getDouble("llv");
            double hhv = tempvalue.getJSONObject("wr").getDouble("hhv");
            hhv = Math.max(hhv, high);
            llv = Math.min(llv, low);
            if (hhv == llv) {
                wr = 0;
            } else {
                wr = 100 * (hhv - zjcj) / (hhv - llv);
            }
        } else {
            wr = 0;
        }
        if (l > 5 && tempvalue.has("wr")) {
            double llv2 = tempvalue.getJSONObject("wr").getDouble("llv2");
            double hhv2 = tempvalue.getJSONObject("wr").getDouble("hhv2");

            hhv2 = Math.max(hhv2, high);
            llv2 = Math.min(llv2, low);
            if (hhv2 == llv2) {
                wr2 = 0;
            } else {
                wr2 = 100 * (hhv2 - zjcj) / (hhv2 - llv2);
            }
        } else {
            wr2 = 0;
        }
        quoteData.getJSONArray("WR").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("WR").getJSONArray(l - 1).put(1, wr);
        quoteData.getJSONArray("WR").getJSONArray(l - 1).put(2, wr2);
    }
    if (indicatorType.equals("VR")) {
        quoteData.getJSONArray("VR").put(new JSONArray());
        double vr = 0, vrma = 0;
        if (l >= 24 && tempvalue.has("vr")) {
            double sum1 = tempvalue.getJSONObject("vr").getDouble("sum1");
            double sum2 = tempvalue.getJSONObject("vr").getDouble("sum2");
            if (zjcj > zrsp) {
                sum1 += cjsl;
            } else {
                sum2 += cjsl;
            }
            if (sum2 == 0) {
                vr = 0;
            } else {
                vr = 100 * sum1 / sum2;
            }
        } else {
            vr = 0;
        }
        if (l >= 6)
            vrma = (tempvalue.getJSONObject("vr").getDouble("sumVr") + vr) / 6;
        quoteData.getJSONArray("VR").getJSONArray(l - 1).put(0, qt);
        quoteData.getJSONArray("VR").getJSONArray(l - 1).put(1, vr);
        quoteData.getJSONArray("VR").getJSONArray(l - 1).put(2, vrma);
    }
}

From source file:com.cssweb.android.view.KlineView.java

private void makeTmpData() throws JSONException {
    JSONObject tempvalue = quoteData.getJSONObject("joTMP");
    int l = quoteData.getJSONArray("K").length();
    double zrsp = quoteData.getDouble("zrsp");
    //      quoteData.getJSONArray("K").getJSONArray(l-1).put(1, tempvalue.getJSONObject(period).getDouble("open")) ;
    //      quoteData.getJSONArray("K").getJSONArray(l-1).put(2, tempvalue.getJSONObject(period).getDouble("high")) ;
    //      quoteData.getJSONArray("K").getJSONArray(l-1).put(3, tempvalue.getJSONObject(period).getDouble("low")) ;
    //      quoteData.getJSONArray("K").getJSONArray(l-1).put(5, tempvalue.getJSONObject(period).getDouble("cjsl")) ;
    //      quoteData.getJSONArray("K").getJSONArray(l-1).put(6, tempvalue.getJSONObject(period).getDouble("cjje")) ;

    Log.i("#####111####", quoteData + ">>>>>>>>>>");
    int tp = quoteData.getInt("tp");
    if (tp == 1) {
        String date = tempvalue.getJSONObject(period).getString("date");
        //Log.i("#####date####", date+">>>>>>>>" + DateTool.isSameWeekMonthYear(date, period));
        if (DateTool.isSameWeekMonthYear(date, period)) {
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(1,
                    tempvalue.getJSONObject(period).getDouble("open"));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(2,
                    tempvalue.getJSONObject(period).getDouble("high"));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(3,
                    tempvalue.getJSONObject(period).getDouble("low"));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(4, zrsp);
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(5,
                    tempvalue.getJSONObject(period).getDouble("cjsl"));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(6,
                    tempvalue.getJSONObject(period).getDouble("cjje"));
            //quoteData.getJSONArray("K").getJSONArray(l-1).put(0, ) ;
            zrsp = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(0);
        }/*from  ww w  . j  av a2 s.  c om*/
    } else {
        int spayday = quoteData.getInt("spday");
        double cjsl = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(5);
        double cjje = quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(6);
        if (spayday == 0) {//0??temp?????
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(2,
                    Math.max(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(2),
                            tempvalue.getJSONObject(period).getDouble("high")));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(3,
                    Math.min(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(3),
                            tempvalue.getJSONObject(period).getDouble("low")));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(5,
                    cjsl + tempvalue.getJSONObject(period).getDouble("cjsl"));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(6,
                    cjje + tempvalue.getJSONObject(period).getDouble("cjje"));
        } else {
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(2,
                    Math.max(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(2),
                            tempvalue.getJSONObject(period).getDouble("high")));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(3,
                    Math.min(quoteData.getJSONArray("K").getJSONArray(l - 1).getDouble(3),
                            tempvalue.getJSONObject(period).getDouble("low")));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(5,
                    tempvalue.getJSONObject(period).getDouble("cjsl"));
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(6,
                    tempvalue.getJSONObject(period).getDouble("cjje"));
        }
        double jrkp = tempvalue.getJSONObject(period).getDouble("open");
        if (jrkp != 0) {
            quoteData.getJSONArray("K").getJSONArray(l - 1).put(1, jrkp);
        }
        zrsp = tempvalue.getJSONObject(period).getDouble("close");
        Log.i("#####222####", quoteData + ">>>>>>>>>>");
    }

    if (mainIndicatorType.toUpperCase().equals("BOLL")) {
        JSONArray jMA = new JSONArray();
        jMA.put(0, 0);
        jMA.put(1, 0);
        jMA.put(2, 0);
        jMA.put(3, 0);
        jMA.put(4, 0);
        jMA.put(5, 0);
        jMA.put(6, 0);
        quoteData.put("BOLL", new JSONArray().put(jMA));
    }

    //Log.i("#######MA#########", quoteData.isNull("MA")+">>>>>>>>>>>" + quoteData);
    if (quoteData.isNull("MA")) {
        JSONArray jMA = new JSONArray();
        jMA.put(0, 0);
        jMA.put(1, 0);
        jMA.put(2, 0);
        jMA.put(3, 0);
        jMA.put(4, 0);
        jMA.put(5, 0);
        jMA.put(6, 0);
        quoteData.put("MA", new JSONArray().put(jMA));
    } else {
        if (quoteData.getJSONArray("MA").length() == 0) {
            JSONArray jMA = new JSONArray();
            jMA.put(0, 0);
            jMA.put(1, 0);
            jMA.put(2, 0);
            jMA.put(3, 0);
            jMA.put(4, 0);
            jMA.put(5, 0);
            jMA.put(6, 0);
            quoteData.put("MA", new JSONArray().put(jMA));
        }
    }
    //Log.i("#######MA#########" + indicatorType.toUpperCase(), quoteData.isNull(indicatorType.toUpperCase())+">>>>>>>>>>>");
    if (quoteData.isNull(indicatorType.toUpperCase())) {
        JSONArray jIn = new JSONArray();
        jIn.put(0, 0);
        jIn.put(1, 0);
        jIn.put(2, 0);
        jIn.put(3, 0);
        quoteData.put(indicatorType.toUpperCase(), new JSONArray().put(jIn));
    }
    if (tempvalue.isNull("ma") && quoteData.getJSONArray("K").length() > 1) //??
        actualDataLen = quoteData.getJSONArray("K").length() - 1;
    else
        actualDataLen = quoteData.getJSONArray("K").length();
    if (actualDataLen < visualKLineCount) {
        count = actualDataLen;
    } else {
        count = visualKLineCount;
    }
    if (this.actualDataLen - this.actualPos - 1 <= this.visualKLineCount) {
        actualPos = actualDataLen - count;
    }
    //      Log.i("#######makeTmpData########" + actualPos, actualDataLen+">>>>>>>>" + count);
}

From source file:com.daskiworks.ghwatch.backend.NotificationStreamParser.java

public static NotificationStream parseNotificationStream(JSONArray json) throws InvalidObjectException {
    NotificationStream ret = new NotificationStream();
    try {/*w  w w .  j  av  a  2  s . com*/

        for (int i = 0; i < json.length(); i++) {
            JSONObject notification = json.getJSONObject(i);

            JSONObject subject = notification.getJSONObject("subject");
            JSONObject repository = notification.getJSONObject("repository");
            String updatedAtStr = Utils.trimToNull(notification.getString("updated_at"));
            Date updatedAt = null;
            try {
                if (updatedAtStr != null) {
                    if (updatedAtStr.endsWith("Z"))
                        updatedAtStr = updatedAtStr.replace("Z", "GMT");
                    updatedAt = df.parse(updatedAtStr);
                }
            } catch (ParseException e) {
                Log.w(TAG, "Invalid date format for value: " + updatedAtStr);
            }

            ret.addNotification(new Notification(notification.getLong("id"), notification.getString("url"),
                    subject.getString("title"), subject.getString("type"), subject.getString("url"),
                    subject.getString("latest_comment_url"), repository.getString("full_name"),
                    repository.getJSONObject("owner").getString("avatar_url"), updatedAt,
                    notification.getString("reason")));

        }
    } catch (Exception e) {
        throw new InvalidObjectException("JSON message is invalid: " + e.getMessage());
    }
    return ret;
}

From source file:net.mandaria.radioreddit.apis.RadioRedditAPI.java

public static RadioStreams GetStreams(Context context, RadioRedditApplication application) {
    RadioStreams radiostreams = new RadioStreams();
    radiostreams.ErrorMessage = "";
    radiostreams.RadioStreams = new ArrayList<RadioStream>();

    try {/*from   w ww.j a  v a  2  s . c  o m*/
        String url = context.getString(R.string.radio_reddit_streams);
        String outputStreams = "";
        boolean errorGettingStreams = false;

        try {
            outputStreams = HTTPUtil.get(context, url);
        } catch (Exception ex) {
            errorGettingStreams = true;
            radiostreams.ErrorMessage = context.getString(R.string.error_RadioRedditServerIsDownNotification);
            application.radioRedditIsDownErrorMessage = radiostreams.ErrorMessage;
            application.isRadioRedditDown = true;
        }

        if (!errorGettingStreams && outputStreams.length() > 0) {
            JSONTokener tokener = new JSONTokener(outputStreams);
            JSONObject json = new JSONObject(tokener);

            JSONObject streams = json.getJSONObject("streams");
            JSONArray streams_names = streams.names();
            ArrayList<RadioStream> list_radiostreams = new ArrayList<RadioStream>();

            // loop through each stream
            for (int i = 0; i < streams.length(); i++) {
                String name = streams_names.getString(i);
                JSONObject stream = streams.getJSONObject(name);

                RadioStream radiostream = new RadioStream();
                radiostream.Name = name;
                // if(stream.has("type"))
                radiostream.Type = stream.getString("type");
                radiostream.Description = stream.getString("description");
                radiostream.Status = stream.getString("status");

                // call status.json to get Relay
                // form url radioreddit.com + status + json
                String status_url = context.getString(R.string.radio_reddit_base_url) + radiostream.Status
                        + context.getString(R.string.radio_reddit_status);

                String outputStatus = "";
                boolean errorGettingStatus = false;

                try {
                    outputStatus = HTTPUtil.get(context, status_url);
                } catch (Exception ex) {
                    errorGettingStatus = true;
                    radiostreams.ErrorMessage = context
                            .getString(R.string.error_RadioRedditServerIsDownNotification);
                }

                //Log.e("RadioReddit", "Length of output: "+ outputStatus.length() + "; Content of output: " + outputStatus);
                // TODO: does  outputStatus.length() > 0 need to be checked here and return a ErrorMessage back and set ErrorGettingStatus = true? 

                if (!errorGettingStatus && outputStatus.length() > 0) {
                    JSONTokener status_tokener = new JSONTokener(outputStatus);
                    JSONObject status_json = new JSONObject(status_tokener);

                    radiostream.Online = Boolean.parseBoolean(status_json.getString("online").toLowerCase());

                    if (radiostream.Online == true) // if offline, no other nodes are available
                    {
                        radiostream.Relay = status_json.getString("relay");

                        list_radiostreams.add(radiostream);
                    }
                }
            }

            // JSON parsing reverses the list for some reason, fixing it...
            if (list_radiostreams.size() > 0) {
                // Sorting will happen later on select station activity
                //Collections.reverse(list_radiostreams);

                radiostreams.RadioStreams = list_radiostreams;
                application.isRadioRedditDown = false;
            } else {
                radiostreams.ErrorMessage = context.getString(R.string.error_NoStreams);
                application.radioRedditIsDownErrorMessage = radiostreams.ErrorMessage;
                application.isRadioRedditDown = true;
            }
        }
    } catch (Exception ex) {
        // We fail to get the streams...
        CustomExceptionHandler ceh = new CustomExceptionHandler(context);
        ceh.sendEmail(ex);

        radiostreams.ErrorMessage = ex.toString();
        ex.printStackTrace();
    }

    return radiostreams;
}

From source file:net.mandaria.radioreddit.apis.RadioRedditAPI.java

public static RadioSong GetCurrentSongInformation(Context context, RadioRedditApplication application) {
    RadioSong radiosong = new RadioSong();
    radiosong.ErrorMessage = "";

    try {// w w w.ja  v a2 s  .  c  o m
        String status_url = context.getString(R.string.radio_reddit_base_url) + application.CurrentStream.Status
                + context.getString(R.string.radio_reddit_status);

        String outputStatus = "";
        boolean errorGettingStatus = false;

        try {
            outputStatus = HTTPUtil.get(context, status_url);
        } catch (Exception ex) {
            ex.printStackTrace();
            errorGettingStatus = true;
            // For now, not used. It is acceptable to error out and not alert the user
            // radiosong.ErrorMessage = context.getString(R.string.error_RadioRedditServerIsDownNotification);
        }

        if (!errorGettingStatus && outputStatus.length() > 0) {
            JSONTokener status_tokener = new JSONTokener(outputStatus);
            JSONObject status_json = new JSONObject(status_tokener);

            radiosong.Playlist = status_json.getString("playlist");

            JSONObject songs = status_json.getJSONObject("songs");
            JSONArray songs_array = songs.getJSONArray("song");

            // get the first song in the array
            JSONObject song = songs_array.getJSONObject(0);
            radiosong.ID = song.getInt("id");
            radiosong.Title = song.getString("title");
            radiosong.Artist = song.getString("artist");
            radiosong.Redditor = song.getString("redditor");
            radiosong.Genre = song.getString("genre");
            radiosong.CumulativeScore = song.getString("score");

            if (radiosong.CumulativeScore.equals("{}"))
                radiosong.CumulativeScore = null;

            radiosong.Reddit_title = song.getString("reddit_title");
            radiosong.Reddit_url = song.getString("reddit_url");
            if (song.has("preview_url"))
                radiosong.Preview_url = song.getString("preview_url");
            if (song.has("download_url"))
                radiosong.Download_url = song.getString("download_url");
            if (song.has("bandcamp_link"))
                radiosong.Bandcamp_link = song.getString("bandcamp_link");
            if (song.has("bandcamp_art"))
                radiosong.Bandcamp_art = song.getString("bandcamp_art");
            if (song.has("itunes_link"))
                radiosong.Itunes_link = song.getString("itunes_link");
            if (song.has("itunes_art"))
                radiosong.Itunes_art = song.getString("itunes_art");
            if (song.has("itunes_price"))
                radiosong.Itunes_price = song.getString("itunes_price");

            // get vote score 
            String reddit_info_url = context.getString(R.string.reddit_link_by)
                    + URLEncoder.encode(radiosong.Reddit_url);

            String outputRedditInfo = "";
            boolean errorGettingRedditInfo = false;

            try {
                outputRedditInfo = HTTPUtil.get(context, reddit_info_url);
            } catch (Exception ex) {
                ex.printStackTrace();
                errorGettingRedditInfo = true;
                // For now, not used. It is acceptable to error out and not alert the user
                // radiosong.ErrorMessage = "Unable to connect to reddit";//context.getString(R.string.error_RadioRedditServerIsDownNotification);
            }

            if (!errorGettingRedditInfo && outputRedditInfo.length() > 0) {
                // Log.e("radio_reddit_test", "Length: " + outputRedditInfo.length());
                // Log.e("radio_reddit_test", "Value: " + outputRedditInfo); // TODO: sometimes the value contains "error: 404", need to check for that. (We can probably safely ignore this for now)
                JSONTokener reddit_info_tokener = new JSONTokener(outputRedditInfo);
                JSONObject reddit_info_json = new JSONObject(reddit_info_tokener);

                JSONObject data = reddit_info_json.getJSONObject("data");

                // default value of score
                String score = context.getString(R.string.vote_to_submit_song);
                String likes = "null";
                String name = "";

                JSONArray children_array = data.getJSONArray("children");

                // Song hasn't been submitted yet
                if (children_array.length() > 0) {
                    JSONObject children = children_array.getJSONObject(0);

                    JSONObject children_data = children.getJSONObject("data");
                    score = children_data.getString("score");

                    likes = children_data.getString("likes");
                    name = children_data.getString("name");
                }

                radiosong.Score = score;
                radiosong.Likes = likes;
                radiosong.Name = name;
            } else {
                radiosong.Score = "?";
                radiosong.Likes = "null";
                radiosong.Name = "";
            }

            return radiosong;
        }
        return null;
    } catch (Exception ex) {
        // We fail to get the current song information...
        CustomExceptionHandler ceh = new CustomExceptionHandler(context);
        ceh.sendEmail(ex);

        ex.printStackTrace();
        radiosong.ErrorMessage = ex.toString();
        return radiosong;
    }
}

From source file:net.mandaria.radioreddit.apis.RadioRedditAPI.java

public static RadioEpisode GetCurrentEpisodeInformation(Context context, RadioRedditApplication application) {
    RadioEpisode radioepisode = new RadioEpisode();
    radioepisode.ErrorMessage = "";

    try {//w  w  w .j  a  va  2s.c om
        String status_url = context.getString(R.string.radio_reddit_base_url) + application.CurrentStream.Status
                + context.getString(R.string.radio_reddit_status);

        String outputStatus = "";
        boolean errorGettingStatus = false;

        try {
            outputStatus = HTTPUtil.get(context, status_url);
        } catch (Exception ex) {
            ex.printStackTrace();
            errorGettingStatus = true;
            // For now, not used. It is acceptable to error out and not alert the user
            // radiosong.ErrorMessage = context.getString(R.string.error_RadioRedditServerIsDownNotification);
        }

        if (!errorGettingStatus && outputStatus.length() > 0) {
            JSONTokener status_tokener = new JSONTokener(outputStatus);
            JSONObject status_json = new JSONObject(status_tokener);

            radioepisode.Playlist = status_json.getString("playlist");

            JSONObject episodes = status_json.getJSONObject("episodes");
            JSONArray episodes_array = episodes.getJSONArray("episode");

            // get the first episode in the array
            JSONObject song = episodes_array.getJSONObject(0);
            radioepisode.ID = song.getInt("id");
            radioepisode.EpisodeTitle = song.getString("episode_title");
            radioepisode.EpisodeDescription = song.getString("episode_description");
            radioepisode.EpisodeKeywords = song.getString("episode_keywords");
            radioepisode.ShowTitle = song.getString("show_title");
            radioepisode.ShowHosts = song.getString("show_hosts").replaceAll(",", ", ");
            radioepisode.ShowRedditors = song.getString("show_redditors").replaceAll(",", ", ");
            radioepisode.ShowGenre = song.getString("show_genre");
            radioepisode.ShowFeed = song.getString("show_feed");
            radioepisode.Reddit_title = song.getString("reddit_title");
            radioepisode.Reddit_url = song.getString("reddit_url");
            if (song.has("preview_url"))
                radioepisode.Preview_url = song.getString("preview_url");
            if (song.has("download_url"))
                radioepisode.Download_url = song.getString("download_url");

            // get vote score
            String reddit_info_url = context.getString(R.string.reddit_link_by)
                    + URLEncoder.encode(radioepisode.Reddit_url);

            String outputRedditInfo = "";
            boolean errorGettingRedditInfo = false;

            try {
                outputRedditInfo = HTTPUtil.get(context, reddit_info_url);
            } catch (Exception ex) {
                ex.printStackTrace();
                errorGettingRedditInfo = true;
                // For now, not used. It is acceptable to error out and not alert the user
                // radiosong.ErrorMessage = "Unable to connect to reddit";//context.getString(R.string.error_RadioRedditServerIsDownNotification);
            }

            if (!errorGettingRedditInfo && outputRedditInfo.length() > 0) {
                // Log.e("radio_reddit_test", "Length: " + outputRedditInfo.length());
                // Log.e("radio_reddit_test", "Value: " + outputRedditInfo); // TODO: sometimes the value contains "error: 404", need to check for that (We can probably safely ignore this for now)
                JSONTokener reddit_info_tokener = new JSONTokener(outputRedditInfo);
                JSONObject reddit_info_json = new JSONObject(reddit_info_tokener);

                JSONObject data = reddit_info_json.getJSONObject("data");

                // default value of score
                String score = context.getString(R.string.vote_to_submit_song);
                String likes = "null";
                String name = "";

                JSONArray children_array = data.getJSONArray("children");

                // Episode hasn't been submitted yet
                if (children_array.length() > 0) {
                    JSONObject children = children_array.getJSONObject(0);

                    JSONObject children_data = children.getJSONObject("data");
                    score = children_data.getString("score");

                    likes = children_data.getString("likes");
                    name = children_data.getString("name");
                }

                radioepisode.Score = score;
                radioepisode.Likes = likes;
                radioepisode.Name = name;
            } else {
                radioepisode.Score = "?";
                radioepisode.Likes = "null";
                radioepisode.Name = "";
            }

            return radioepisode;
        }
        return null;
    } catch (Exception ex) {
        // We fail to get the current song information...
        CustomExceptionHandler ceh = new CustomExceptionHandler(context);
        ceh.sendEmail(ex);

        ex.printStackTrace();
        radioepisode.ErrorMessage = ex.toString();
        return radioepisode;
    }

}

From source file:net.mandaria.radioreddit.apis.RadioRedditAPI.java

public static List<RadioSong> GetTopChartsByType(Context context, RadioRedditApplication application,
        String type) {//from   w  w  w.j  av  a 2s.c  o m
    List<RadioSong> radiosongs = new ArrayList<RadioSong>();

    try {
        String chart_url = "";

        if (type.equals("all"))
            chart_url = context.getString(R.string.radio_reddit_charts_all);
        else if (type.equals("month"))
            chart_url = context.getString(R.string.radio_reddit_charts_month);
        else if (type.equals("week"))
            chart_url = context.getString(R.string.radio_reddit_charts_week);
        else if (type.equals("day"))
            chart_url = context.getString(R.string.radio_reddit_charts_day);

        // TODO: might could merge this code with GetCurrentSongInformation

        String outputStatus = "";
        boolean errorGettingStatus = false;

        try {
            outputStatus = HTTPUtil.get(context, chart_url);
        } catch (Exception ex) {
            ex.printStackTrace();
            errorGettingStatus = true;
            // For now, not used. It is acceptable to error out and not alert the user
            // radiosong.ErrorMessage = context.getString(R.string.error_RadioRedditServerIsDownNotification);
        }

        if (!errorGettingStatus && outputStatus.length() > 0) {
            JSONTokener status_tokener = new JSONTokener(outputStatus);
            JSONObject status_json = new JSONObject(status_tokener);

            JSONObject songs = status_json.getJSONObject("songs");
            JSONArray songs_array = songs.getJSONArray("song");

            // get the first song in the array
            for (int i = 0; i < songs_array.length(); i++) {
                RadioSong radiosong = new RadioSong();
                radiosong.ErrorMessage = "";

                JSONObject song = songs_array.getJSONObject(i);
                //radiosong.ID = song.getInt("id");
                radiosong.Title = song.getString("title");
                radiosong.Artist = song.getString("artist");
                radiosong.Redditor = song.getString("redditor");
                radiosong.Genre = song.getString("genre");
                //radiosong.CumulativeScore = song.getString("score");

                //if(radiosong.CumulativeScore.equals("{}"))
                //   radiosong.CumulativeScore = null;   

                radiosong.Reddit_title = song.getString("reddit_title");
                radiosong.Reddit_url = song.getString("reddit_url");
                if (song.has("preview_url"))
                    radiosong.Preview_url = song.getString("preview_url");
                if (song.has("download_url"))
                    radiosong.Download_url = song.getString("download_url");
                if (song.has("bandcamp_link"))
                    radiosong.Bandcamp_link = song.getString("bandcamp_link");
                if (song.has("bandcamp_art"))
                    radiosong.Bandcamp_art = song.getString("bandcamp_art");
                if (song.has("itunes_link"))
                    radiosong.Itunes_link = song.getString("itunes_link");
                if (song.has("itunes_art"))
                    radiosong.Itunes_art = song.getString("itunes_art");
                if (song.has("itunes_price"))
                    radiosong.Itunes_price = song.getString("itunes_price");

                radiosongs.add(radiosong);
            }
        }
    } catch (Exception ex) {
        // We fail to get the current song information...
        CustomExceptionHandler ceh = new CustomExceptionHandler(context);
        ceh.sendEmail(ex);

        ex.printStackTrace();
        // TODO: return error message?? Might need to wrap List<RadioSong> in an object that has an ErrorMessage data member
        //radiosong.ErrorMessage = ex.toString();
        //return radiosong;
    }
    return radiosongs;
}