Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created September 27, 2017 07:56
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ssaurel/765d3a3b5e3ee3535ca20579214a2eb9 to your computer and use it in GitHub Desktop.
Bitcoin Price Index Watcher App for the SSaurel's Blog
package com.ssaurel.bitcoinwatcher;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
public static final String BPI_ENDPOINT = "https://api.coindesk.com/v1/bpi/currentprice.json";
private OkHttpClient okHttpClient = new OkHttpClient();
private ProgressDialog progressDialog;
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.txt);
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("BPI Loading");
progressDialog.setMessage("Wait ...");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_load) {
load();
}
return super.onOptionsItemSelected(item);
}
private void load() {
Request request = new Request.Builder()
.url(BPI_ENDPOINT)
.build();
progressDialog.show();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Toast.makeText(MainActivity.this, "Error during BPI loading : "
+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call call, Response response)
throws IOException {
final String body = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
parseBpiResponse(body);
}
});
}
});
}
private void parseBpiResponse(String body) {
try {
StringBuilder builder = new StringBuilder();
JSONObject jsonObject = new JSONObject(body);
JSONObject timeObject = jsonObject.getJSONObject("time");
builder.append(timeObject.getString("updated")).append("\n\n");
JSONObject bpiObject = jsonObject.getJSONObject("bpi");
JSONObject usdObject = bpiObject.getJSONObject("USD");
builder.append(usdObject.getString("rate")).append("$").append("\n");
JSONObject gbpObject = bpiObject.getJSONObject("GBP");
builder.append(gbpObject.getString("rate")).append("£").append("\n");
JSONObject euroObject = bpiObject.getJSONObject("EUR");
builder.append(euroObject.getString("rate")).append("€").append("\n");
txt.setText(builder.toString());
} catch (Exception e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment