List of usage examples for org.json JSONObject isNull
public boolean isNull(String key)
From source file:com.cssweb.android.view.KlineView.java
public void drawRSI(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen) throws JSONException { if (quoteData.isNull("RSI") || quoteData.getJSONArray("RSI").length() < 1) { isNetworkErrorDisplay();//from w w w . j a v a 2 s . com return; } double max = 100.0; double min = 0.0; int len = quoteData.getJSONArray("RSI").length(); for (int i = begin; i < (begin + count) && i < len; i++) { double k = quoteData.getJSONArray("RSI").getJSONArray(i).getDouble(1); double d = quoteData.getJSONArray("RSI").getJSONArray(i).getDouble(2); double j = quoteData.getJSONArray("RSI").getJSONArray(i).getDouble(3); if (k > max) max = k; if (d > max) max = d; if (j > max) max = j; if (k < 0) { if (k < min) min = k; } if (d < 0) { if (d < min) min = d; } if (j < 0) { if (j < min) min = j; } } double scale = this.volumeHeight / (max + Math.abs(min)); int startX = klineX; int startY = klineY; int axisY100 = (int) (startY + volumeHeight - (100 + Math.abs(min)) * scale); paint.setColor(GlobalColor.clrLine); Graphics.drawDashline(canvas, startX, axisY100, width, axisY100, paint); String axisLabel1 = ""; if (zs) axisLabel1 = "100.00"; else axisLabel1 = "100.00"; int axisY80 = (int) (startY + volumeHeight - (80 + Math.abs(min)) * scale); Graphics.drawDashline(canvas, startX, axisY80, width, axisY80, paint); String axisLabel2 = "80.00"; int axisY50 = (int) (startY + volumeHeight - (50 + Math.abs(min)) * scale); Graphics.drawDashline(canvas, startX, axisY50, width, axisY50, paint); String axisLabel3 = "50.00"; int axisY20 = (int) (startY + volumeHeight - (20 + Math.abs(min)) * scale); Graphics.drawDashline(canvas, startX, axisY20, width, axisY20, paint); String axisLabel4 = "20.00"; int axisY0 = (int) (startY + volumeHeight - (0 + Math.abs(min)) * scale); Graphics.drawDashline(canvas, startX, axisY0, width, axisY0, paint); if (isTrackStatus == false) { tPaint.setColor(GlobalColor.colorKlabel); canvas.drawText(axisLabel1, startX - tips / 4, (float) (axisY100 + axisLabelHeight / 2), tPaint); canvas.drawText(axisLabel2, startX - tips / 4, (float) (axisY80 + axisLabelHeight / 2), tPaint); canvas.drawText(axisLabel3, startX - tips / 4, (float) (axisY50 + axisLabelHeight / 2), tPaint); canvas.drawText(axisLabel4, startX - tips / 4, (float) (axisY20 + axisLabelHeight / 2), tPaint); } canvas.drawLine(klineX, height - axisLabelHeight, width, height - axisLabelHeight, paint); if (quoteData != null) { double axisX = klineX; double bias1x = klineX; double bias1y = 0; double bias2x = klineX; double bias2y = 0; double bias3x = klineX; double bias3y = 0; for (int i = begin; i < (begin + count); i++) { double rsi1 = quoteData.getJSONArray("RSI").getJSONArray(i).getDouble(1); double rsi2 = quoteData.getJSONArray("RSI").getJSONArray(i).getDouble(2); double rsi3 = quoteData.getJSONArray("RSI").getJSONArray(i).getDouble(3); double bias1Height = 0; double bias2Height = 0; double bias3Height = 0; bias1Height = scale * rsi1; bias2Height = scale * rsi2; bias3Height = scale * rsi3; //klineX = 0; if ((i - begin) == 0) axisX = axisX + spaceWidth; else axisX = axisX + spaceWidth + shapeWidth; if ((i - begin) == 0) { bias1x = bias1x + spaceWidth + shapeWidth / 2; bias1y = height - axisLabelHeight - bias1Height; } else { double x1 = bias1x; double y1 = bias1y; bias1x = bias1x + spaceWidth + shapeWidth; bias1y = height - axisLabelHeight - bias1Height; paint.setColor(GlobalColor.colorM5); canvas.drawLine((int) Math.round(x1), (int) y1, (int) Math.round(bias1x), (int) bias1y, paint); } if ((i - begin) == 0) { bias2x = bias2x + spaceWidth + shapeWidth / 2; bias2y = height - axisLabelHeight - bias2Height; } else { double x1 = bias2x; double y1 = bias2y; bias2x = bias2x + spaceWidth + shapeWidth; bias2y = height - axisLabelHeight - bias2Height; paint.setColor(GlobalColor.colorM10); canvas.drawLine((int) Math.round(x1), (int) y1, (int) Math.round(bias2x), (int) bias2y, paint); } if ((i - begin) == 0) { bias3x = bias3x + spaceWidth + shapeWidth / 2; bias3y = height - axisLabelHeight - bias3Height; } else { double x1 = bias3x; double y1 = bias3y; bias3x = bias3x + spaceWidth + shapeWidth; bias3y = height - axisLabelHeight - bias3Height; paint.setARGB(255, 255, 0, 255); canvas.drawLine((int) Math.round(x1), (int) y1, (int) Math.round(bias3x), (int) bias3y, paint); } if (i == (begin + count - 1)) { //labelDif.text = "RSI6:" + Utils.StockFormat(exchange+stockCode, rsi1); //labelDea.text = "RSI12:" + Utils.StockFormat(exchange+stockCode, rsi2); //labelMacd.text = "RSI24:" + Utils.StockFormat(exchange+stockCode, rsi3); } } // end for } // end if }
From source file:com.cssweb.android.view.KlineView.java
public void drawOBV(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen) throws JSONException { // this.shapeWidth = shapeWidth; // this.spaceWidth = spaceWidth; if (quoteData.isNull("OBV") || quoteData.getJSONArray("OBV").length() < 1) { isNetworkErrorDisplay();// w ww. j av a 2 s. c o m return; } long max = 1; long min = 99999999999999l; long obv, maobv; int len = quoteData.getJSONArray("OBV").length(); for (int i = begin; i < (begin + count) && i < len; i++) { obv = quoteData.getJSONArray("OBV").getJSONArray(i).getLong(1); maobv = quoteData.getJSONArray("OBV").getJSONArray(i).getLong(2); max = Arith.max(obv, maobv, max); min = Arith.min(obv, maobv, min); } max = max + (max - min) / 10; min = min - (max - min) / 10; double scale = (double) (this.volumeHeight) / (max - min); int startX = klineX; int startY = klineY; double AxisLabelVolume = max; String lblvalue = "12345"; int ratiolen = String.valueOf(Math.round(AxisLabelVolume)).length() - String.valueOf(lblvalue).length(); String labelRatio = ""; int scaleVol = 1; switch (ratiolen) { case 1: labelRatio = "x10"; scaleVol = 10; break; case 2: labelRatio = "x100"; scaleVol = 100; break; case 3: labelRatio = "x1000"; scaleVol = 1000; break; case 4: labelRatio = "x10000"; scaleVol = 10000; break; case 5: labelRatio = "x1"; scaleVol = 1000000; break; case 6: labelRatio = "x1"; scaleVol = 1000000; break; default: labelRatio = "x1"; scaleVol = 1; break; } int axisY1 = (int) (startY + volumeHeight - (max - min) * scale); paint.setColor(GlobalColor.clrLine); Graphics.drawDashline(canvas, startX, axisY1, width, axisY1, paint); String axisLabel1 = String.valueOf((int) max / scaleVol); int axisY2 = (int) (startY + volumeHeight - (max - min) * 3 / 4 * scale); Graphics.drawDashline(canvas, startX, axisY2, width, axisY2, paint); String axisLabel2 = String.valueOf((int) (min + (max - min) * 3 / 4) / scaleVol); int axisY3 = (int) (startY + volumeHeight - (max - min) * 2 / 4 * scale); Graphics.drawDashline(canvas, startX, axisY3, width, axisY3, paint); String axisLabel3 = String.valueOf((int) (min + (max - min) * 2 / 4) / scaleVol); int axisY4 = (int) (startY + volumeHeight - (max - min) / 4 * scale); Graphics.drawDashline(canvas, startX, axisY4, width, axisY4, paint); String axisLabel4 = String.valueOf((int) (min + (max - min) * 1 / 4) / scaleVol); if (isTrackStatus == false) { tPaint.setColor(GlobalColor.colorKlabel); canvas.drawText(labelRatio, startX - tips / 4, startY + volumeHeight + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel1, startX - tips / 4, axisY1 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel2, startX - tips / 4, axisY2 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel3, startX - tips / 4, axisY3 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel4, startX - tips / 4, axisY4 + axisLabelHeight / 2, tPaint); } canvas.drawLine(startX, height - axisLabelHeight, width, height - axisLabelHeight, paint); if (quoteData != null) { double currX = 0, currY1 = 0, currY2 = 0;//??? double nextX = 0, nextY1 = 0, nextY2 = 0;//?? int kHeight = 0; for (int i = begin; i < (begin + count) && i < len; i++) { obv = quoteData.getJSONArray("OBV").getJSONArray(i).getLong(1); maobv = quoteData.getJSONArray("OBV").getJSONArray(i).getLong(2); startX = klineX; if ((i - begin) == 0) { currX = 0; currX = nextX = currX + startX + spaceWidth + shapeWidth / 2; kHeight = (int) ((obv - min) * scale); currY1 = nextY1 = startY + volumeHeight - kHeight; kHeight = (int) ((maobv - min) * scale); currY2 = nextY2 = startY + volumeHeight - kHeight; } else { nextX = currX + spaceWidth + shapeWidth; } // obv kHeight = (int) ((obv - min) * scale); nextY1 = startY + volumeHeight - kHeight; paint.setColor(GlobalColor.colorM5); canvas.drawLine((int) currX, (int) currY1, (int) nextX, (int) nextY1, paint); // maobv kHeight = (int) ((maobv - min) * scale); nextY2 = startY + volumeHeight - kHeight; paint.setColor(GlobalColor.colorM10); canvas.drawLine((int) currX, (int) currY2, (int) nextX, (int) nextY2, paint); currX = nextX; currY1 = nextY1; currY2 = nextY2; if (i == (begin + count - 1)) { } } // end for } // end if }
From source file:com.cssweb.android.view.KlineView.java
public void drawROC(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen, String indicator) throws JSONException { if (quoteData.isNull(indicator) || quoteData.getJSONArray(indicator).length() < 1) { isNetworkErrorDisplay();/* ww w. j a v a2 s . co m*/ return; } this.shapeWidth = shapeWidth; this.spaceWidth = spaceWidth; double max = 0.001; double min = 9999999999f; double wr = 0, wr2 = 0; int len = quoteData.getJSONArray(indicator).length(); for (int i = begin; i < (begin + count) && i < len; i++) { wr = quoteData.getJSONArray(indicator).getJSONArray(i).getDouble(1); wr2 = quoteData.getJSONArray(indicator).getJSONArray(i).getDouble(2); max = Arith.max(wr, wr2, max); min = Arith.min(wr, wr2, min); } max = max * 1.1; min = min - Math.abs(min) * 0.1; double scale = (double) (this.volumeHeight) / (max - min); int startX = klineX; int startY = klineY; double AxisLabelVolume = max; String lblvalue = "12345"; int ratiolen = String.valueOf(Math.round(AxisLabelVolume)).length() - String.valueOf(lblvalue).length(); String labelRatio = ""; int scaleVol = 1; switch (ratiolen) { case 1: labelRatio = "x10"; scaleVol = 10; break; case 2: labelRatio = "x100"; scaleVol = 100; break; case 3: labelRatio = "x1000"; scaleVol = 1000; break; case 4: labelRatio = "x10000"; scaleVol = 10000; break; default: labelRatio = ""; scaleVol = 1; break; } int axisY1 = (int) (startY + volumeHeight - (max - min) * scale); paint.setColor(GlobalColor.clrLine); Graphics.drawDashline(canvas, startX, axisY1, width, axisY1, paint); String axisLabel1 = Utils.dataFormation(max / scaleVol, 1); int axisY2 = (int) (startY + volumeHeight - (max - min) * 3 / 4 * scale); Graphics.drawDashline(canvas, startX, axisY2, width, axisY2, paint); String axisLabel2 = Utils.dataFormation((min + (max - min) * 3 / 4) / scaleVol, 1); int axisY3 = (int) (startY + volumeHeight - (max - min) * 2 / 4 * scale); Graphics.drawDashline(canvas, startX, axisY3, klineX + klineWidth, axisY3, paint); String axisLabel3 = Utils.dataFormation((min + (max - min) * 2 / 4) / scaleVol, 1); int axisY4 = (int) (startY + volumeHeight - (max - min) / 4 * scale); Graphics.drawDashline(canvas, startX, axisY4, klineX + klineWidth, axisY4, paint); String axisLabel4 = Utils.dataFormation((min + (max - min) * 1 / 4) / scaleVol, 1); if (isTrackStatus == false) { tPaint.setColor(GlobalColor.colorKlabel); canvas.drawText(labelRatio, startX - tips / 4, startY + volumeHeight + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel1, startX - tips / 4, axisY1 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel2, startX - tips / 4, axisY2 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel3, startX - tips / 4, axisY3 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel4, startX - tips / 4, axisY4 + axisLabelHeight / 2, tPaint); } canvas.drawLine(startX, height - axisLabelHeight, width, height - axisLabelHeight, paint); if (quoteData != null) { double currX = 0, currY1 = 0, currY2 = 0;//??? double nextX = 0, nextY1 = 0, nextY2 = 0;//?? int kHeight = 0; for (int i = begin; i < (begin + count) && i < len; i++) { wr = quoteData.getJSONArray(indicator).getJSONArray(i).getDouble(1); wr2 = quoteData.getJSONArray(indicator).getJSONArray(i).getDouble(2); if ((i - begin) == 0) { currX = startX; currX = nextX = currX + spaceWidth + shapeWidth / 2; kHeight = (int) ((wr - min) * scale); currY1 = nextY1 = startY + volumeHeight - kHeight; kHeight = (int) ((wr2 - min) * scale); currY2 = nextY2 = startY + volumeHeight - kHeight; } else { nextX = currX + spaceWidth + shapeWidth; kHeight = (int) ((wr - min) * scale); nextY1 = startY + volumeHeight - kHeight; kHeight = (int) ((wr2 - min) * scale); nextY2 = startY + volumeHeight - kHeight; } paint.setColor(GlobalColor.colorM5); canvas.drawLine((float) currX, (float) currY1, (float) nextX, (float) nextY1, paint); paint.setColor(GlobalColor.colorM10); canvas.drawLine((float) currX, (float) currY2, (float) nextX, (float) nextY2, paint); currX = nextX; currY1 = nextY1; currY2 = nextY2; if (i == (begin + count - 1)) { } } // end for } // end if }
From source file:com.cssweb.android.view.KlineView.java
public void drawOther(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen, String indicator) throws JSONException { if (quoteData.isNull(indicator) || quoteData.getJSONArray(indicator).length() < 1) { isNetworkErrorDisplay();/*from ww w . j a v a 2s . c o m*/ return; } this.shapeWidth = shapeWidth; this.spaceWidth = spaceWidth; double max = 0.001; double min = 9999999999f; double wr = 0, wr2 = 0; int len = quoteData.getJSONArray(indicator).length(); for (int i = begin; i < (begin + count) && i < len; i++) { wr = quoteData.getJSONArray(indicator).getJSONArray(i).getDouble(1); wr2 = quoteData.getJSONArray(indicator).getJSONArray(i).getDouble(2); max = Arith.max(wr, wr2, max); min = Arith.min(wr, wr2, min); } max = max + (max - min) / 10; min = min - (max - min) / 10; double scale = (double) (this.volumeHeight) / (max - min); int startX = klineX; int startY = klineY; double AxisLabelVolume = max; String lblvalue = "12345"; int ratiolen = String.valueOf(Math.round(AxisLabelVolume)).length() - String.valueOf(lblvalue).length(); String labelRatio = ""; int scaleVol = 1; switch (ratiolen) { case 1: labelRatio = "x10"; scaleVol = 10; break; case 2: labelRatio = "x100"; scaleVol = 100; break; case 3: labelRatio = "x1000"; scaleVol = 1000; break; case 4: labelRatio = "x10000"; scaleVol = 10000; break; default: labelRatio = ""; scaleVol = 1; break; } int axisY1 = (int) (startY + volumeHeight - (max - min) * scale); paint.setColor(GlobalColor.clrLine); Graphics.drawDashline(canvas, startX, axisY1, width, axisY1, paint); String axisLabel1 = String.valueOf((int) max / scaleVol); int axisY2 = (int) (startY + volumeHeight - (max - min) * 3 / 4 * scale); Graphics.drawDashline(canvas, startX, axisY2, width, axisY2, paint); String axisLabel2 = String.valueOf((int) (min + (max - min) * 3 / 4) / scaleVol); int axisY3 = (int) (startY + volumeHeight - (max - min) * 2 / 4 * scale); Graphics.drawDashline(canvas, startX, axisY3, klineX + klineWidth, axisY3, paint); String axisLabel3 = String.valueOf((int) (min + (max - min) * 2 / 4) / scaleVol); int axisY4 = (int) (startY + volumeHeight - (max - min) / 4 * scale); Graphics.drawDashline(canvas, startX, axisY4, klineX + klineWidth, axisY4, paint); String axisLabel4 = String.valueOf((int) (min + (max - min) * 1 / 4) / scaleVol); if (isTrackStatus == false) { tPaint.setColor(GlobalColor.colorKlabel); canvas.drawText(labelRatio, startX - tips / 4, startY + volumeHeight + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel1, startX - tips / 4, axisY1 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel2, startX - tips / 4, axisY2 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel3, startX - tips / 4, axisY3 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel4, startX - tips / 4, axisY4 + axisLabelHeight / 2, tPaint); } canvas.drawLine(startX, height - axisLabelHeight, width, height - axisLabelHeight, paint); if (quoteData != null) { double currX = 0, currY1 = 0, currY2 = 0;//??? double nextX = 0, nextY1 = 0, nextY2 = 0;//?? int kHeight = 0; for (int i = begin; i < (begin + count) && i < len; i++) { wr = quoteData.getJSONArray(indicator).getJSONArray(i).getLong(1); wr2 = quoteData.getJSONArray(indicator).getJSONArray(i).getLong(2); if ((i - begin) == 0) { currX = startX; currX = nextX = currX + spaceWidth + shapeWidth / 2; kHeight = (int) ((wr - min) * scale); currY1 = nextY1 = startY + volumeHeight - kHeight; kHeight = (int) ((wr2 - min) * scale); currY2 = nextY2 = startY + volumeHeight - kHeight; } else { nextX = currX + spaceWidth + shapeWidth; kHeight = (int) ((wr - min) * scale); nextY1 = startY + volumeHeight - kHeight; kHeight = (int) ((wr2 - min) * scale); nextY2 = startY + volumeHeight - kHeight; } paint.setColor(GlobalColor.colorM5); canvas.drawLine((float) currX, (float) currY1, (float) nextX, (float) nextY1, paint); paint.setColor(GlobalColor.colorM10); canvas.drawLine((float) currX, (float) currY2, (float) nextX, (float) nextY2, paint); currX = nextX; currY1 = nextY1; currY2 = nextY2; if (i == (begin + count - 1)) { } } // end for } // end if }
From source file:com.cssweb.android.view.KlineView.java
/** * ??//from ww w. ja va 2 s . c o 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); }/* w ww. java2 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:io.teak.sdk.RemoteConfiguration.java
public static void requestConfigurationForApp(final Session session) { HashMap<String, Object> payload = new HashMap<>(); payload.put("id", session.appConfiguration.appId); new Thread(new Request("gocarrot.com", "/games/" + session.appConfiguration.appId + "/settings.json", payload, session) {//from ww w .j a v a2 s . co m @Override protected void done(int responseCode, String responseBody) { try { JSONObject response = new JSONObject(responseBody); RemoteConfiguration configuration = new RemoteConfiguration(session.appConfiguration, response.isNull("auth") ? "gocarrot.com" : response.getString("auth"), nullInsteadOfEmpty(response.isNull("sdk_sentry_dsn") ? null : response.getString("sdk_sentry_dsn")), nullInsteadOfEmpty(response.isNull("app_sentry_dsn") ? null : response.getString("app_sentry_dsn"))); synchronized (eventListenersMutex) { for (EventListener e : RemoteConfiguration.eventListeners) { e.onConfigurationReady(configuration); } } } catch (Exception e) { Log.e(LOG_TAG, "Error processing settings.json " + Log.getStackTraceString(e)); } } }).start(); }
From source file:com.imaginary.home.cloud.device.Device.java
static void mapDevice(@Nonnull ControllerRelay relay, @Nonnull JSONObject json, @Nonnull Map<String, Object> state) throws JSONException { state.put("relayId", relay.getControllerRelayId()); state.put("deviceId", UUID.randomUUID().toString()); if (json.has("name") && !json.isNull("name")) { state.put("name", json.getString("name")); }/*w w w. ja va 2 s. c o m*/ if (json.has("description") && !json.isNull("description")) { state.put("description", json.getString("description")); } if (json.has("model") && !json.isNull("model")) { state.put("model", json.getString("model")); } if (json.has("deviceId") && !json.isNull("deviceId")) { state.put("vendorDeviceId", json.getString("deviceId")); } if (json.has("systemId") && !json.isNull("systemId")) { state.put("homeAutomationSystemId", json.getString("systemId")); } }
From source file:org.openmidaas.library.authentication.AuthCallbackForAccessToken.java
private void obtainAccessToken(SubjectToken subjectToken, String deviceToken) throws JSONException { AVSServer.getAuthToken(subjectToken, deviceToken, new AsyncHttpResponseHandler() { @Override/*ww w . jav a 2 s . c om*/ public void onSuccess(String response) { if (response == null || response.isEmpty()) { MIDaaS.logError(TAG, "Server response is empty."); mCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } else { try { JSONObject accessToken = new JSONObject(response); if ((accessToken.has(Constants.AccessTokenKeys.ACCESS_TOKEN) && !(accessToken.isNull(Constants.AccessTokenKeys.ACCESS_TOKEN))) && (accessToken.has(Constants.AccessTokenKeys.EXPIRES_IN) && !(accessToken.isNull(Constants.AccessTokenKeys.EXPIRES_IN)))) { AccessToken token = AccessToken.createAccessToken( accessToken.getString(Constants.AccessTokenKeys.ACCESS_TOKEN), accessToken.getInt(Constants.AccessTokenKeys.EXPIRES_IN)); if (token != null) { MIDaaS.logDebug(TAG, "got access token: "); mCallback.onSuccess(token); } else { MIDaaS.logError(TAG, "Error could not create access token"); mCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } } else { MIDaaS.logError(TAG, "Server response is not what is expected"); mCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } } catch (JSONException e) { MIDaaS.logError(TAG, "Internal error while parsing server JSON response"); mCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } } } @Override public void onFailure(Throwable e, String response) { MIDaaS.logError(TAG, "Server responded with error " + response); mCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } }); }
From source file:org.openmidaas.library.authentication.AuthCallbackForRegistration.java
@Override public void onSuccess(String deviceToken) { try {/* w w w. j a va2s .c o m*/ AVSServer.registerDevice(deviceToken, new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { if (response == null || response.isEmpty()) { mInitCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } else { try { MIDaaS.logDebug(TAG, "device successfully registered. persisting registration."); JSONObject responseObject = new JSONObject(response); if (responseObject.has("subjectToken") && !(responseObject.isNull("subjectToken"))) { SubjectToken subjectToken = SubjectTokenFactory.createAttribute(); subjectToken.setValue(Build.MODEL); subjectToken.setSignedToken(responseObject.getString("subjectToken")); subjectToken.save(); // if we didn't get the access token, we can get it on-demand at a later time. if ((responseObject.has(Constants.AccessTokenKeys.ACCESS_TOKEN) && !(responseObject.isNull(Constants.AccessTokenKeys.ACCESS_TOKEN))) && (responseObject.has(Constants.AccessTokenKeys.EXPIRES_IN) && !(responseObject .isNull(Constants.AccessTokenKeys.EXPIRES_IN)))) { MIDaaS.logDebug(TAG, "Registration response has an access token."); AccessToken token = AccessToken.createAccessToken( responseObject.getString(Constants.AccessTokenKeys.ACCESS_TOKEN), responseObject.getInt(Constants.AccessTokenKeys.EXPIRES_IN)); if (token != null) { MIDaaS.logDebug(TAG, "Access token is ok."); AuthenticationManager.getInstance().setAccessToken(token); } else { MIDaaS.logError(TAG, "Access token is null."); mInitCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } } else { MIDaaS.logDebug(TAG, "No access token object in server response. Access token will be created on-demand."); } } else { MIDaaS.logError(TAG, "Server response doesn't match expected response"); mInitCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } mInitCallback.onSuccess(); } catch (InvalidAttributeValueException e) { // should never get here b/c we're returning true. MIDaaS.logError(TAG, "logic error. should never have thrown exception"); } catch (MIDaaSException e) { MIDaaS.logError(TAG, e.getError().getErrorMessage()); mInitCallback.onError(e); } catch (JSONException e) { MIDaaS.logError(TAG, e.getMessage()); mInitCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } } } @Override public void onFailure(Throwable e, String response) { MIDaaS.logError(TAG, response); mInitCallback.onError(new MIDaaSException(MIDaaSError.SERVER_ERROR)); } }); } catch (JSONException e) { MIDaaS.logError(TAG, "Internal error"); MIDaaS.logError(TAG, e.getMessage()); mInitCallback.onError(null); } }