Android™
Android™
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// Replace with your actual Finnhub API key.
String apiKey = "YOUR_FINNHUB_API_KEY";
String symbol = "AAPL";
CkHttp http = new CkHttp();
// This is the URL without params.
String urlWithoutParams = "https://finnhub.io/api/v1/quote";
CkHttpRequest req = new CkHttpRequest();
// Add params that will be sent in the URL.
req.AddParam("symbol",symbol);
req.AddParam("token",apiKey);
req.put_HttpVerb("GET");
// Send the request to get the JSON response.
CkHttpResponse resp = new CkHttpResponse();
success = http.HttpReq(urlWithoutParams,req,resp);
if (success == false) {
Log.i(TAG, http.lastErrorText());
return;
}
CkJsonObject json = new CkJsonObject();
resp.GetBodyJson(json);
int statusCode = resp.get_StatusCode();
Log.i(TAG, "response status code: " + String.valueOf(statusCode));
json.put_EmitCompact(false);
Log.i(TAG, json.emit());
// Sample result:
// {
// "c": 248.8,
// "d": -4.09,
// "dp": -1.6173,
// "h": 255.493,
// "l": 248.07,
// "o": 253.9,
// "pc": 252.89,
// "t": 1774641600
// }
if (statusCode == 200) {
// Add the symbol to the top of the result.
json.AddStringAt(0,"symbol",symbol);
// Rename members for clarification.
json.Rename("c","currentPrice");
json.Rename("d","change");
json.Rename("dp","percentChange");
json.Rename("h","high");
json.Rename("l","low");
json.Rename("o","open");
json.Rename("pc","prevClose");
json.Rename("t","unixTime");
Log.i(TAG, json.emit());
}
else {
Log.i(TAG, "Failed");
}
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}