List of usage examples for org.json JSONObject getJSONArray
public JSONArray getJSONArray(String key) throws JSONException
From source file:GUI.simplePanel.java
public void textParser(String input) { boolean served = false; jTextArea1.setText(input + "\n" + jTextArea1.getText()); getjLabel1().setText("Awaiting Command"); if (input.contains("temperature")) { try {/*from ww w . java2 s.c o m*/ String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices"); JSONObject obj; obj = new JSONObject(reply); JSONArray services = obj.getJSONArray("services"); boolean found = false; for (int i = 0; i < services.length(); i++) { if (found) { served = true; return; } Object pref = services.getJSONObject(i).get("url"); String url = (String) pref; if (url.contains("temp")) { // http://127.0.0.1:8181/sensor/1/temp String serviceHost = (url.split(":")[1].substring(2)); int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]); String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/"))); String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath); JSONObject temperature; obj = new JSONObject(serviceReply); String temp = obj.getJSONObject("sensor").getString("Temperature"); if (temp.length() > 4) { JOptionPane.showMessageDialog(self, "Temperature is " + temp.substring(0, temp.indexOf(".") + 2) + " Celsius"); } else { JOptionPane.showMessageDialog(self, "Temperature is " + temp + " Celsius"); } found = true; } } JOptionPane.showMessageDialog(self, "I can't know! There are no sensors for that!"); } catch (Exception e) { e.printStackTrace(); } } if (input.contains("light") || input.startsWith("li")) { try { String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices"); JSONObject obj; obj = new JSONObject(reply); JSONArray services = obj.getJSONArray("services"); boolean found = false; for (int i = 0; i < services.length(); i++) { if (found) { served = true; return; } Object pref = services.getJSONObject(i).get("url"); String url = (String) pref; if (url.contains("light")) { // http://127.0.0.1:8181/sensor/1/temp String serviceHost = (url.split(":")[1].substring(2)); int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]); String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/"))); String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath); JSONObject temperature; obj = new JSONObject(serviceReply); String temp = obj.getJSONObject("sensor").getString("Light"); JOptionPane.showMessageDialog(self, "Light levels are at " + temp + " of 1023 "); found = true; } } JOptionPane.showMessageDialog(self, "I can't know! There are no sensors for that!"); } catch (Exception e) { e.printStackTrace(); } } if ((input.contains("turn on") || input.contains("turn off")) && input.contains("number")) { int numberIndex = input.indexOf("number ") + "number ".length(); String number = input.substring(numberIndex).split(" ")[0]; if (number.equals("for") || number.equals("four")) { number = "4"; } if (number.equals("to") || number.equals("two") || number.equals("cho")) { number = "2"; } if (number.equals("one")) { number = "1"; } if (number.equals("three")) { number = "3"; } try { String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices"); JSONObject obj; obj = new JSONObject(reply); JSONArray services = obj.getJSONArray("services"); boolean found = false; for (int i = 0; i < services.length(); i++) { if (found) { served = true; return; } Object pref = services.getJSONObject(i).get("url"); String url = (String) pref; if (url.contains("sensor/" + number)) { // http://127.0.0.1:8181/sensor/1/temp String serviceHost = (url.split(":")[1].substring(2)); int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]); String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/"))); String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath); JSONObject temperature; obj = new JSONObject(serviceReply); String temp = obj.getJSONObject("sensor").getString("Switch"); if (!(temp.equals("0") || temp.equals("1"))) { JOptionPane.showMessageDialog(self, "Sensor does not provide a switch service man"); } else if (input.contains("turn on") && temp.equals("1")) { JOptionPane.showMessageDialog(self, "Switch is already on at sensor " + number + "!"); } else if (input.contains("turn off") && temp.equals("0")) { JOptionPane.showMessageDialog(self, "Switch is already off at sensor " + number + "!"); } else if (input.contains("turn on") && temp.equals("0")) { String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "", "/sensor/" + number + "/switch"); JOptionPane.showMessageDialog(self, "Request for switch sent"); } else if (input.contains("turn off") && temp.equals("1")) { String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "", "/sensor/" + number + "/switch"); JOptionPane.showMessageDialog(self, "Request for switch sent"); } found = true; } } } catch (Exception e) { e.printStackTrace(); } } if (input.contains("change") && input.contains("number")) { int numberIndex = input.indexOf("number ") + "number ".length(); String number = input.substring(numberIndex).split(" ")[0]; if (number.equals("for") || number.equals("four")) { number = "4"; } if (number.equals("to") || number.equals("two") || number.equals("cho")) { number = "2"; } if (number.equals("one")) { number = "1"; } if (number.equals("three")) { number = "3"; } try { String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices"); JSONObject obj; obj = new JSONObject(reply); JSONArray services = obj.getJSONArray("services"); boolean found = false; for (int i = 0; i < services.length(); i++) { if (found) { served = true; return; } Object pref = services.getJSONObject(i).get("url"); String url = (String) pref; if (url.contains("sensor/" + number)) { // http://127.0.0.1:8181/sensor/1/temp String serviceHost = (url.split(":")[1].substring(2)); int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]); String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/"))); String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath); JSONObject temperature; obj = new JSONObject(serviceReply); String temp = obj.getJSONObject("sensor").getString("Switch"); if (!(temp.equals("0") || temp.equals("1"))) { JOptionPane.showMessageDialog(self, "Sensor does not provide a switch service man"); } else { String serviceReply2 = util.httpRequest.sendPost(serviceHost, Port, "", "/sensor/" + number + "/switch"); JOptionPane.showMessageDialog(self, "Request for switch sent"); } found = true; } } } catch (Exception e) { JOptionPane.showMessageDialog(self, e.getLocalizedMessage()); } } if (input.contains("get all")) { try { String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices"); JSONObject obj; served = true; obj = new JSONObject(reply); JSONArray services = obj.getJSONArray("services"); boolean found = false; String servicesString = ""; for (int i = 0; i < services.length(); i++) { Object pref = services.getJSONObject(i).get("url"); String url = (String) pref; servicesString += url + "\n"; } JOptionPane.showMessageDialog(self, servicesString); } catch (Exception e) { e.printStackTrace(); } } if (input.contains("humidity") || input.startsWith("humidity")) { try { String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices"); JSONObject obj; obj = new JSONObject(reply); JSONArray services = obj.getJSONArray("services"); boolean found = false; for (int i = 0; i < services.length(); i++) { if (found) { served = true; return; } Object pref = services.getJSONObject(i).get("url"); String url = (String) pref; if (url.contains("humi")) { // http://127.0.0.1:8181/sensor/1/temp String serviceHost = (url.split(":")[1].substring(2)); int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]); String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/"))); String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath); JSONObject temperature; obj = new JSONObject(serviceReply); String temp = obj.getJSONObject("sensor").getString("Humidity"); JOptionPane.showMessageDialog(self, "Humidity levels are at " + temp + " of 1023 "); found = true; } } JOptionPane.showMessageDialog(self, "I can't know! There are no sensors for that!"); } catch (Exception e) { e.printStackTrace(); } } if (input.contains("pressure") || input.startsWith("pressure")) { try { String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices"); JSONObject obj; obj = new JSONObject(reply); JSONArray services = obj.getJSONArray("services"); boolean found = false; for (int i = 0; i < services.length(); i++) { if (found) { served = true; return; } Object pref = services.getJSONObject(i).get("url"); String url = (String) pref; if (url.contains("pres")) { // http://127.0.0.1:8181/sensor/1/temp String serviceHost = (url.split(":")[1].substring(2)); int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]); String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/"))); String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath); JSONObject temperature; obj = new JSONObject(serviceReply); String temp = obj.getJSONObject("sensor").getString("Pressure"); JOptionPane.showMessageDialog(self, "Pressure levels are at " + temp + " of 1023 "); found = true; } } JOptionPane.showMessageDialog(self, "I can't know! There are no sensors for that!"); } catch (Exception e) { e.printStackTrace(); } } if (!served) { try { ChatterBotFactory factory = new ChatterBotFactory(); ChatterBot bot1 = factory.create(CLEVERBOT); ChatterBotSession bot1session = bot1.createSession(); String s = input; String response = bot1session.think(s); JOptionPane.showMessageDialog(self, response, "Jarvis says", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { } } }
From source file:GUI.simplePanel.java
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed try {//from w ww.j ava 2s . c o m String reply = util.httpRequest.sendPost("127.0.0.1", 8383, "", "/getServices"); JSONObject obj; obj = new JSONObject(reply); JSONArray services = obj.getJSONArray("services"); boolean found = false; for (int i = 0; i < services.length(); i++) { if (found) { return; } Object pref = services.getJSONObject(i).get("url"); String url = (String) pref; if (url.contains("temp")) { // http://127.0.0.1:8181/sensor/1/temp String serviceHost = (url.split(":")[1].substring(2)); int Port = Integer.parseInt((url.split(":")[2]).split("/")[0]); String servicePath = (url.split(":")[2].substring(url.split(":")[2].indexOf("/"))); String serviceReply = util.httpRequest.sendPost(serviceHost, Port, "", servicePath); JSONObject temperature; obj = new JSONObject(serviceReply); String temp = obj.getJSONObject("sensor").getString("Temperature"); JOptionPane.showMessageDialog(self, "Temperature is " + temp.substring(0, temp.indexOf(".") + 2) + " Celsius"); found = true; } } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.sakisds.icymonitor.activities.AddNotificationActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_notification); mSettings = getSharedPreferences(MainViewActivity.SHAREDPREFS_FILE, 0); // Prepare buttons findViewById(R.id.button_add).setOnClickListener(this); findViewById(R.id.button_cancel).setOnClickListener(this); findViewById(R.id.button_notification_help).setOnClickListener(this); // Prepare spinners mDeviceSpinner = (Spinner) findViewById(R.id.spinner_device); mTypeSpinner = (Spinner) findViewById(R.id.spinner_type); mSensorSpinner = (Spinner) findViewById(R.id.spinner_sensor); mConditionSpinner = (Spinner) findViewById(R.id.spinner_condition); // Edittext//from w ww. jav a2 s . c om mSensorValue = (EditText) findViewById(R.id.editText_value); // Checkbox mRingOnceCheckbox = (CheckBox) findViewById(R.id.checkbox_notif_ringonce); // URL mURL = getIntent().getExtras().getString(MainViewActivity.EXTRA_ADDRESS); // Client mClient.setMaxRetriesAndTimeout(2, 2000); // Retrieve sensors AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.put("type", "listdev"); params.put("id", String.valueOf(mSettings.getLong("device_id", -2))); final Activity context = this; final ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.retrieving_devices), false); mClient.get(mURL, params, new JsonHttpResponseHandler() { @Override public void onSuccess(JSONObject response) { try { // System JSONArray systemResponse = response.getJSONArray("System"); int len = systemResponse.length(); int lenTemp = 0; int lenFan = 0; int lenVolt = 0; for (int i = 0; i < len; i++) { Sensor sensor = new Sensor(systemResponse.getJSONObject(i)); if (sensor.getType().equals("Temperature")) { lenTemp++; } else if (sensor.getType().equals("Fan")) { lenFan++; } else if (sensor.getType().equals("Voltage")) { lenVolt++; } } mSystemTempSensors = new Sensor[lenTemp]; mSystemFanSensors = new Sensor[lenFan]; mSystemVoltSensors = new Sensor[lenVolt]; int iTemp = 0; int iFan = 0; int iVolt = 0; for (int i = 0; i < len; i++) { Sensor sensor = new Sensor(systemResponse.getJSONObject(i)); if (sensor.getType().equals("Temperature")) { mSystemTempSensors[iTemp] = sensor; iTemp++; } else if (sensor.getType().equals("Fan")) { mSystemFanSensors[iFan] = sensor; iFan++; } else if (sensor.getType().equals("Voltage")) { mSystemVoltSensors[iVolt] = sensor; iVolt++; } } // CPU JSONArray cpuResponse = response.getJSONArray("CPU"); len = cpuResponse.length(); lenTemp = 0; int lenLoad = 0; int lenClock = 0; int lenPower = 0; for (int i = 0; i < len; i++) { Sensor sensor = new Sensor(cpuResponse.getJSONObject(i)); if (sensor.getType().equals("Temperature")) { lenTemp++; } else if (sensor.getType().equals("Load")) { lenLoad++; } else if (sensor.getType().equals("Clock")) { lenClock++; } else if (sensor.getType().equals("Power")) { lenPower++; } } mCPUTempSensors = new Sensor[lenTemp]; mCPULoadSensors = new Sensor[lenLoad]; mCPUPowerSensors = new Sensor[lenPower]; mCPUClockSensors = new Sensor[lenClock]; iTemp = 0; int iLoad = 0; int iPower = 0; int iClock = 0; for (int i = 0; i < len; i++) { Sensor sensor = new Sensor(cpuResponse.getJSONObject(i)); if (sensor.getType().equals("Temperature")) { mCPUTempSensors[iTemp] = sensor; iTemp++; } else if (sensor.getType().equals("Power")) { mCPUPowerSensors[iPower] = sensor; iPower++; } else if (sensor.getType().equals("Clock")) { mCPUClockSensors[iClock] = sensor; iClock++; } else if (sensor.getType().equals("Load")) { mCPULoadSensors[iLoad] = sensor; iLoad++; } } // GPU JSONArray gpuResponse = response.getJSONArray("GPU"); len = gpuResponse.length(); lenTemp = 0; lenLoad = 0; lenClock = 0; lenFan = 0; for (int i = 0; i < len; i++) { Sensor sensor = new Sensor(gpuResponse.getJSONObject(i)); if (sensor.getType().equals("Temperature")) { lenTemp++; } else if (sensor.getType().equals("Load")) { lenLoad++; } else if (sensor.getType().equals("Clock")) { lenClock++; } else if (sensor.getType().equals("Fan")) { lenFan++; } } mGPUTempSensors = new Sensor[lenTemp]; mGPULoadSensors = new Sensor[lenLoad]; mGPUFanSensors = new Sensor[lenFan]; mGPUClockSensors = new Sensor[lenClock]; iTemp = 0; iLoad = 0; iClock = 0; iFan = 0; for (int i = 0; i < len; i++) { Sensor sensor = new Sensor(gpuResponse.getJSONObject(i)); if (sensor.getType().equals("Temperature")) { mGPUTempSensors[iTemp] = sensor; iTemp++; } else if (sensor.getType().equals("Fan")) { mGPUFanSensors[iFan] = sensor; iFan++; } else if (sensor.getType().equals("Clock")) { mGPUClockSensors[iClock] = sensor; iClock++; } else if (sensor.getType().equals("Load")) { mGPULoadSensors[iLoad] = sensor; iLoad++; } } // Prepare spinners setAdapter(mSensorSpinner, mSystemTempSensors); mTypeSpinner.setOnItemSelectedListener(new TypeOnItemSelectedListener()); mDeviceSpinner.setOnItemSelectedListener(new DeviceOnItemSelectedListener()); // Close window progress.hide(); } catch (JSONException e) { Toast.makeText(context, getString(R.string.error_could_not_reach_host), Toast.LENGTH_LONG) .show(); progress.hide(); } } @Override public void onFailure(Throwable error, String content) { Toast.makeText(context, getString(R.string.error_could_not_reach_host) + ":" + error.getMessage(), Toast.LENGTH_LONG).show(); finish(); progress.hide(); Log.e("Notif", error.getMessage()); } }); }
From source file:org.loklak.api.geo.GeocodeServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Query post = RemoteAccess.evaluate(request); // manage DoS if (post.isDoS_blackout()) { response.sendError(503, "your request frequency is too high"); return;// w w w. j a v a2 s . co m } // parameters String callback = post.get("callback", ""); boolean jsonp = callback != null && callback.length() > 0; boolean minified = post.get("minified", false); String data = post.get("data", ""); String places = post.get("places", ""); if (places.length() == 0 && data.length() == 0) { response.sendError(503, "you must submit a data attribut with a json containing the property 'places' with a list of place names"); return; } String[] place = new String[0]; if (places.length() > 0) { place = places.split(","); } else { // parse the json data try { JSONObject json = new JSONObject(data); if (json.has("places") && json.get("places") instanceof JSONArray) { JSONArray p = json.getJSONArray("places"); place = new String[p.length()]; int i = 0; for (Object o : p) place[i++] = (String) o; } else { response.sendError(400, "submitted data is not well-formed: expected a list of strings"); return; } } catch (IOException e) { Log.getLog().warn(e); } } // find locations for places JSONObject locations = new JSONObject(true); for (String p : place) { GeoMark loc = DAO.geoNames.analyse(p, null, 5, Long.toString(System.currentTimeMillis())); if (loc != null) { locations.put(p, loc.toJSON(minified)); } else { locations.put(p, new JSONObject()); } } post.setResponse(response, "application/javascript"); // generate json JSONObject m = new JSONObject(true); m.put("locations", locations); // write json response.setCharacterEncoding("UTF-8"); PrintWriter sos = response.getWriter(); if (jsonp) sos.print(callback + "("); sos.print(m.toString(minified ? 0 : 2)); if (jsonp) sos.println(");"); sos.println(); post.finalize(); }
From source file:edu.cwru.apo.Home.java
public void onRestRequestComplete(Methods method, JSONObject result) { if (method == Methods.phone) { if (result != null) { try { String requestStatus = result.getString("requestStatus"); if (requestStatus.compareTo("success") == 0) { SharedPreferences.Editor editor = getSharedPreferences(APO.PREF_FILE_NAME, MODE_PRIVATE) .edit();//from ww w .j a va 2 s. c o m editor.putLong("updateTime", result.getLong("updateTime")); editor.commit(); int numbros = result.getInt("numBros"); JSONArray caseID = result.getJSONArray("caseID"); JSONArray first = result.getJSONArray("first"); JSONArray last = result.getJSONArray("last"); JSONArray phone = result.getJSONArray("phone"); JSONArray family = result.getJSONArray("family"); ContentValues values; for (int i = 0; i < numbros; i++) { values = new ContentValues(); values.put("_id", caseID.getString(i)); values.put("first", first.getString(i)); values.put("last", last.getString(i)); values.put("phone", phone.getString(i)); values.put("family", family.getString(i)); database.replace("phoneDB", null, values); } } else if (requestStatus.compareTo("timestamp invalid") == 0) { Toast msg = Toast.makeText(this, "Invalid timestamp. Please try again.", Toast.LENGTH_LONG); msg.show(); } else if (requestStatus.compareTo("HMAC invalid") == 0) { Auth.loggedIn = false; Toast msg = Toast.makeText(this, "You have been logged out by the server. Please log in again.", Toast.LENGTH_LONG); msg.show(); finish(); } else { Toast msg = Toast.makeText(this, "Invalid requestStatus", Toast.LENGTH_LONG); msg.show(); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else if (method == Methods.checkAppVersion) { if (result != null) { try { String requestStatus = result.getString("requestStatus"); if (requestStatus.compareTo("success") == 0) { String appVersion = result.getString("version"); String appDate = result.getString("date"); final String appUrl = result.getString("url"); PackageInfo pinfo = this.getPackageManager().getPackageInfo(getPackageName(), 0); ; if (appVersion.compareTo(pinfo.versionName) != 0) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Upgrade"); builder.setMessage("Update available, ready to upgrade?"); builder.setIcon(R.drawable.icon); builder.setCancelable(false); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent promptInstall = new Intent("android.intent.action.VIEW", Uri.parse("https://apo.case.edu:8090/app/" + appUrl)); startActivity(promptInstall); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } else { Toast msg = Toast.makeText(this, "No updates found", Toast.LENGTH_LONG); msg.show(); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
From source file:com.cssweb.android.view.KlineViewSingle.java
public void initData(JSONObject quoteData) throws JSONException { if (!quoteData.isNull("K")) { this.quoteData = quoteData; this.actualDataLen = quoteData.getJSONArray("K").length(); if (quoteData.isNull("joTMP") && actualDataLen > 1) {//temp?? Log.i(">>>>>temp??K??1>>>>>>", ">>>>>>>>>>>>>>>>>>>>" + quoteData); this.actualDataLen = quoteData.getJSONArray("K").length() - 1; } else {/*from w ww . ja v a 2s.c o m*/ makeTodayData(); } } }
From source file:com.cssweb.android.view.KlineViewSingle.java
public void drawVOLUME(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen) throws JSONException { if (quoteData == null || quoteData.isNull("MA")) { isNetworkErrorDisplay();// www . j a v a 2 s .c o m return; } axisX = klineX; for (int i = begin; i < (begin + count); i++) { highVolume = Arith.max(highVolume, quoteData.getJSONArray("MA").getJSONArray(i).getDouble(5), quoteData.getJSONArray("MA").getJSONArray(i).getDouble(6)); } if (highVolume > 0) { scale = volumeHeight / highVolume; } else { scale = 999999; } double ratio = highVolume / rowNum; ratio = Math.round(ratio); String lblvalue = "12345", labelRatio = ""; String lbhighVolume = String.valueOf(Math.round(highVolume)); int ratiolen = lbhighVolume.length() - lblvalue.length(); double 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 = "x1"; scaleVol = 10000; break; case 5: labelRatio = "x1"; scaleVol = 1000000; break; case 6: labelRatio = "x1"; scaleVol = 1000000; break; default: labelRatio = "x1"; scaleVol = 1; break; } double AxisLabelVolume = 0; paint.setColor(GlobalColor.clrLine); tPaint.setTextAlign(Paint.Align.RIGHT); for (int i = 0; i <= rowNum; i++) { if (i == rowNum || i == 0) { canvas.drawLine(klineX, klineY + rowHeight * i, width, klineY + rowHeight * i, paint); } else { Graphics.drawDashline(canvas, klineX, klineY + rowHeight * i, width, klineY + rowHeight * i, paint); } if (i != rowNum && isTrackStatus == false) { AxisLabelVolume = Math.round(highVolume - ratio * i); if (i == 0) { tPaint.setColor(GlobalColor.colorTicklabel); canvas.drawText(Utils.dataFormation(Math.round(AxisLabelVolume / scaleVol), 0), klineX - tips / 4, klineY + rowHeight * i + axisLabelHeight / 2, tPaint); } else { tPaint.setColor(GlobalColor.colorTicklabel); canvas.drawText(Utils.dataFormation(Math.round(AxisLabelVolume / scaleVol), 0), klineX - tips / 4, klineY + rowHeight * i + axisLabelHeight / 2, tPaint); } } } if (isTrackStatus == false) { tPaint.setColor(GlobalColor.colorTicklabel); canvas.drawText(labelRatio, klineX - tips / 4, height - axisLabelHeight, tPaint); } // K if (quoteData != null) { for (int i = begin; i < (begin + count); i++) { if (i == 0) { drawVolumeKLine(canvas, i - begin, quoteData.getJSONArray("K").getJSONArray(i).getDouble(1), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4), quoteData.getJSONArray("K").getJSONArray(i).getDouble(5)); } else { drawVolumeKLine(canvas, i - begin, quoteData.getJSONArray("K").getJSONArray(i).getDouble(1), quoteData.getJSONArray("K").getJSONArray(i).getDouble(4), quoteData.getJSONArray("K").getJSONArray(i).getDouble(5)); } if (i == (begin + count - 1)) { // Util.drawString(g2, labelVolume, "VOLUME:", css); } } drawVolumeMACD(canvas, quoteData, begin, count, shapeWidth, spaceWidth, highPrice, lowPrice, highVolume); } }
From source file:com.cssweb.android.view.KlineViewSingle.java
private void drawMACD(Canvas canvas, JSONObject quoteData, int begin, int count, double shapeWidth, double spaceWidth, double highPrice, double lowPrice, double highVolume, int actualDataLen) throws JSONException { if (quoteData == null || quoteData.isNull("MACD")) { isNetworkErrorDisplay();/*w w w. j av a 2 s . c o m*/ return; } double max = 0.001; double min = -0.001; int len = quoteData.getJSONArray("MACD").length(); for (int i = begin; i < (begin + count) && i < len; i++) { double dif = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(1); double dea = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(2); double macd = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(3); if (dif > max) max = dif; if (dea > max) max = dea; if (macd > max) max = macd; if (dif < 0) { if (dif < min) min = dif; } if (dea < 0) { if (dea < min) min = dea; } if (macd < 0) { if (macd < min) min = macd; } } double upHeight = 0; double downHeight = 0; upHeight = volumeHeight * (max / (max + Math.abs(min))); downHeight = volumeHeight * (Math.abs(min) / (max + Math.abs(min))); if (isTrackStatus == false) { String axisLabel1 = ""; if (zs) axisLabel1 = Utils.dataFormation(max, 0); else axisLabel1 = Utils.dataFormation(max, stockdigit); tPaint.setColor(GlobalColor.colorKlabel); canvas.drawText(axisLabel1, klineX - tips / 4, klineY, tPaint); canvas.drawText(Utils.dataFormation(0, stockdigit), klineX - tips / 4, (int) (klineY + upHeight), tPaint); String axisLabel3 = ""; if (zs) axisLabel3 = Utils.dataFormation(min, 0); else axisLabel3 = Utils.dataFormation(min, stockdigit); canvas.drawText(axisLabel3, klineX - tips / 4, (klineY + volumeHeight), tPaint); } paint.setColor(GlobalColor.clrLine); Graphics.drawDashline(canvas, klineX, klineY, width, klineY, paint); Graphics.drawDashline(canvas, klineX, (int) (klineY + upHeight), width, (int) (klineY + upHeight), paint); canvas.drawLine(klineX, height - axisLabelHeight, width, height - axisLabelHeight, paint); if (quoteData != null) { double axisX = klineX; double dif = 0, dea = 0, macd = 0, difx1 = 0, difx2 = 0, deax1 = 0, deax2 = 0; int dify1 = 0, dify2 = 0, deay1 = 0, deay2 = 0; for (int i = begin; i < (begin + count) && i < len; i++) { dif = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(1); dea = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(2); macd = quoteData.getJSONArray("MACD").getJSONArray(i).getDouble(3); double difScale = 0; double deaScale = 0; double macdScale = 0; double difHeight = 0; double deaHeight = 0; double macdHeight = 0; if (dif >= 0) { difScale = upHeight / max; } else { difScale = downHeight / Math.abs(min); } if (dea >= 0) { deaScale = upHeight / max; } else { deaScale = downHeight / Math.abs(min); } if (macd >= 0) { macdScale = upHeight / max; } else { macdScale = downHeight / Math.abs(min); } if ((i - begin) == 0) axisX = axisX + spaceWidth; else axisX = axisX + spaceWidth + shapeWidth; if ((i - begin) == 0) { difx2 = difx2 + klineX + spaceWidth + shapeWidth / 2; difHeight = Math.abs(dif) * difScale; if (dif >= 0) { dify2 = (int) (klineY + upHeight - difHeight); } else { dify2 = (int) (klineY + upHeight + difHeight); } } else { difx1 = difx2; dify1 = dify2; difx2 = difx2 + spaceWidth + shapeWidth; difHeight = Math.abs(dif) * difScale; if (dif >= 0) { dify2 = (int) (klineY + upHeight - difHeight); } else { dify2 = (int) (klineY + upHeight + difHeight); } paint.setColor(GlobalColor.colorM5); canvas.drawLine((float) difx1, (float) dify1, (float) difx2, (float) dify2, paint); } if ((i - begin) == 0) { deax2 = deax2 + klineX + spaceWidth + shapeWidth / 2; deaHeight = Math.abs(dea) * deaScale; if (dea >= 0) { deay2 = (int) (klineY + upHeight - deaHeight); } else { deay2 = (int) (klineY + upHeight + deaHeight); } } else { deax1 = deax2; deay1 = deay2; deax2 = deax2 + spaceWidth + shapeWidth; deaHeight = Math.abs(dea) * deaScale; if (dea >= 0) { deay2 = (int) (klineY + upHeight - deaHeight); } else { deay2 = (int) (klineY + upHeight + deaHeight); } paint.setColor(GlobalColor.colorM10); canvas.drawLine((float) deax1, (float) deay1, (float) deax2, (float) deay2, paint); } if (macd >= 0) { macdHeight = Math.abs(macd) * macdScale; paint.setColor(GlobalColor.colorKdown); canvas.drawLine((int) (axisX + shapeWidth / 2), (int) (klineY + upHeight - macdHeight), (int) (axisX + shapeWidth / 2), (int) (klineY + upHeight), paint); } else { macdHeight = Math.abs(macd) * macdScale; paint.setARGB(255, 84, 255, 255); canvas.drawLine((int) (axisX + shapeWidth / 2), (int) (klineY + upHeight), (int) (axisX + shapeWidth / 2), (int) (klineY + upHeight + macdHeight), paint); } if (i == (begin + count - 1)) { // } } // end for } // end if }
From source file:com.cssweb.android.view.KlineViewSingle.java
public void drawKDJ(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("KDJ") || quoteData.getJSONArray("KDJ").length() < 1) { isNetworkErrorDisplay();//from ww w. j a v a2 s . c om return; } //this.shapeWidth = shapeWidth; //this.spaceWidth = spaceWidth; double max = 0.001; double min = -0.001; int len = quoteData.getJSONArray("KDJ").length(); for (int i = begin; i < (begin + count) && i < len; i++) { double k = quoteData.getJSONArray("KDJ").getJSONArray(i).getDouble(1); double d = quoteData.getJSONArray("KDJ").getJSONArray(i).getDouble(2); double j = quoteData.getJSONArray("KDJ").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; } } max = Math.max(max, 100); min = Math.min(min, 0.0); double scale = this.volumeHeight / (Math.max(max, 100) + 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 = "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); String axisLabel5 = "0.00"; if (isTrackStatus == false) { tPaint.setColor(GlobalColor.colorKlabel); canvas.drawText(axisLabel1, startX - tips / 4, axisY100 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel2, startX - tips / 4, axisY80 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel3, startX - tips / 4, axisY50 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel4, startX - tips / 4, axisY20 + axisLabelHeight / 2, tPaint); canvas.drawText(axisLabel5, startX - tips / 4, axisY0 + axisLabelHeight / 2, tPaint); } canvas.drawLine(startX, height - axisLabelHeight, width, height - axisLabelHeight, paint); if (quoteData != null) { double axisX = klineX; double difx = klineX; double deax = klineX; double jx = klineX; int kHeight = 0; int ky = 0; int dHeight = 0; int dy = 0; int jHeight = 0; int jy = 0; for (int i = begin; i < (begin + count); i++) { double k = quoteData.getJSONArray("KDJ").getJSONArray(i).getDouble(1); double d = quoteData.getJSONArray("KDJ").getJSONArray(i).getDouble(2); double j = quoteData.getJSONArray("KDJ").getJSONArray(i).getDouble(3); //klineX = 0; if ((i - begin) == 0) axisX = axisX + spaceWidth; else axisX = axisX + spaceWidth + shapeWidth; if ((i - begin) == 0) { difx = difx + spaceWidth + shapeWidth / 2; kHeight = (int) ((k + Math.abs(min)) * scale); ky = startY + volumeHeight - kHeight; } else { double x1 = difx; double y1 = ky; difx = difx + spaceWidth + shapeWidth; kHeight = (int) ((k + Math.abs(min)) * scale); ky = startY + volumeHeight - kHeight; paint.setColor(GlobalColor.colorM5); canvas.drawLine((int) Math.round(x1), (int) y1, (int) Math.round(difx), (int) ky, paint); } if ((i - begin) == 0) { deax = deax + spaceWidth + shapeWidth / 2; dHeight = (int) ((d + Math.abs(min)) * scale); dy = startY + volumeHeight - dHeight; } else { double x1 = deax; double y1 = dy; deax = deax + spaceWidth + shapeWidth; dHeight = (int) ((d + Math.abs(min)) * scale); dy = startY + volumeHeight - dHeight; paint.setColor(GlobalColor.colorM10); canvas.drawLine((int) Math.round(x1), (int) y1, (int) Math.round(deax), (int) dy, paint); } if ((i - begin) == 0) { jx = jx + spaceWidth + shapeWidth / 2; jHeight = (int) ((j + Math.abs(min)) * scale); jy = startY + volumeHeight - jHeight; } else { double x1 = jx; double y1 = jy; jx = jx + spaceWidth + shapeWidth; jHeight = (int) ((j + Math.abs(min)) * scale); jy = startY + volumeHeight - jHeight; paint.setARGB(255, 255, 0, 255); canvas.drawLine((int) Math.round(x1), (int) y1, (int) Math.round(jx), (int) jy, paint); } if (i == (begin + count - 1)) { //labelDif.text = "K:" + Utils.StockFormat(exchange+stockCode, k); //labelDea.text = "D:" + Utils.StockFormat(exchange+stockCode, d); //labelMacd.text = "J:" + Utils.StockFormat(exchange+stockCode, j); } } // end for } // end if }
From source file:com.cssweb.android.view.KlineViewSingle.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();/* w w w .j av a 2s. c om*/ 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 }