Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected Void doInBackground(Void... arg0) {
- databasehandler sh = new databasehandler();
- // Making a request to url and getting response
- String jsonStr = sh.makeServiceCall(url);
- Log.e(TAG, "Response from url: " + jsonStr);
- if (jsonStr != null) {
- try {
- JSONArray jsonArray = new JSONArray(jsonStr);
- // looping through currencies
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject c = jsonArray.getJSONObject(i);
- String name = c.getString("name");
- String symbol = c.getString("symbol");
- Double price_usd = c.getDouble("price_usd");
- Double price_eur = c.getDouble("price_eur");
- Double price_btc = c.getDouble("price_btc");
- Double volume_eur = c.getDouble("volume_eur");
- Double market_cap_usd = c.getDouble("market_cap_usd");
- Double percent_change_1h = c.getDouble("percent_change_1h");
- Double percent_change_24h = c.getDouble("percent_change_24h");
- Double percent_change_7d = c.getDouble("percent_change_7d");
- // tmp hash map for single contact
- HashMap<String, String> currency = new HashMap<>();
- // adding each child node to HashMap key => value
- currency.put("name", name);
- currency.put("symbol", symbol);
- // adding currency to currencyList
- currencyList.add(currency);
- }
- } catch (final JSONException e) {
- Log.e(TAG, "Json parsing error: " + e.getMessage());
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(),
- "Json parsing error: " + e.getMessage(),
- Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- } else {
- Log.e(TAG, "Couldn't get json from server.");
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(),
- "Couldn't get json from server. Check LogCat for possible errors!",
- Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- // Dismiss the progress dialog
- if (pDialog.isShowing())
- pDialog.dismiss();
- /**
- * Updating parsed JSON data into ListView
- * */
- ListAdapter adapter = new SimpleAdapter(
- currencyTableView.this, currencyList,
- R.layout.list_crypto_items, new String[]{"name", "symbol"}, new int[]{R.id.name,
- R.id.symbol});
- lv.setAdapter(adapter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement