Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. protected Void doInBackground(Void... arg0) {
  2. databasehandler sh = new databasehandler();
  3.  
  4. // Making a request to url and getting response
  5. String jsonStr = sh.makeServiceCall(url);
  6.  
  7. Log.e(TAG, "Response from url: " + jsonStr);
  8.  
  9. if (jsonStr != null) {
  10. try {
  11. JSONArray jsonArray = new JSONArray(jsonStr);
  12.  
  13.  
  14. // looping through currencies
  15. for (int i = 0; i < jsonArray.length(); i++) {
  16. JSONObject c = jsonArray.getJSONObject(i);
  17.  
  18. String name = c.getString("name");
  19. String symbol = c.getString("symbol");
  20. Double price_usd = c.getDouble("price_usd");
  21. Double price_eur = c.getDouble("price_eur");
  22. Double price_btc = c.getDouble("price_btc");
  23. Double volume_eur = c.getDouble("volume_eur");
  24. Double market_cap_usd = c.getDouble("market_cap_usd");
  25. Double percent_change_1h = c.getDouble("percent_change_1h");
  26. Double percent_change_24h = c.getDouble("percent_change_24h");
  27. Double percent_change_7d = c.getDouble("percent_change_7d");
  28.  
  29.  
  30. // tmp hash map for single contact
  31. HashMap<String, String> currency = new HashMap<>();
  32.  
  33. // adding each child node to HashMap key => value
  34.  
  35. currency.put("name", name);
  36. currency.put("symbol", symbol);
  37.  
  38. // adding currency to currencyList
  39. currencyList.add(currency);
  40. }
  41. } catch (final JSONException e) {
  42. Log.e(TAG, "Json parsing error: " + e.getMessage());
  43. runOnUiThread(new Runnable() {
  44. @Override
  45. public void run() {
  46. Toast.makeText(getApplicationContext(),
  47. "Json parsing error: " + e.getMessage(),
  48. Toast.LENGTH_LONG)
  49. .show();
  50. }
  51. });
  52.  
  53. }
  54. } else {
  55. Log.e(TAG, "Couldn't get json from server.");
  56. runOnUiThread(new Runnable() {
  57. @Override
  58. public void run() {
  59. Toast.makeText(getApplicationContext(),
  60. "Couldn't get json from server. Check LogCat for possible errors!",
  61. Toast.LENGTH_LONG)
  62. .show();
  63. }
  64. });
  65.  
  66. }
  67.  
  68. return null;
  69. }
  70.  
  71. @Override
  72. protected void onPostExecute(Void result) {
  73. super.onPostExecute(result);
  74. // Dismiss the progress dialog
  75. if (pDialog.isShowing())
  76. pDialog.dismiss();
  77. /**
  78. * Updating parsed JSON data into ListView
  79. * */
  80. ListAdapter adapter = new SimpleAdapter(
  81. currencyTableView.this, currencyList,
  82. R.layout.list_crypto_items, new String[]{"name", "symbol"}, new int[]{R.id.name,
  83. R.id.symbol});
  84.  
  85. lv.setAdapter(adapter);
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement