

Android: consume a REST / HTTPs service using JSON and Threads
source link: https://marco.dev/2016/01/27/android-consume-a-rest-https-service-using-json-and-threads/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

java-version.com: What's new in Java 16? 15? Keep up to date!
Android: consume a REST / HTTPs service using JSON and Threads
GitHub: https://github.com/marco76/stockVoiceTicker
In this example we show how to implement some Android features:
- Text-to-speech
- Read a JSON web stream
- Use a thread to communicate with a website (REST)
- Timer
The application is very limited and has been created for educational purpose only.
1. Read a webpage content (REST/HTTPs)
With Android if you want to read a webpage you have to create a thread. Android doesn’t allow you to open an URL connection from the Main (thread) because it could be resource intensive and time consuming. If you try to use the main Thread you will get this exception android.os.NetworkOnMainThreadException.
In our case we are calling a Yahoo service using YQL with the following URL : https://query.yahooapis.com/v1/public/yql?q=select%20LastTradePriceOnly%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22GOOG%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
And we wait an answer in JSON format similar to this one.
{"query":{"count":1,"created":"2016-01-27T08:42:41Z","lang":"en-US","results":{"quote":{"LastTradePriceOnly":"713.04"}}}}
In our application we create a Task in the main thread using ExecutorService and Future:
private String readFromWeb() { // THREAD final ExecutorService service; // this Task will contact Yahoo and store the answer (web page) in a String final Future<String> task; String jsonString = null;
The Future task will receive the result of the HTTPS request and it will store it in a String (using task.get()).
service = Executors.newFixedThreadPool(1); task = service.submit(new YahooQuote(YAHOO_URL_PRE + ticker.getText() + YAHOO_URL_POST)); try { jsonString = task.get(); } catch (final InterruptedException | ExecutionException ex) { ex.printStackTrace(); } finally { service.shutdownNow(); }
The class (YahooQuote) that communicate with Yahoo implements Callable and must override the call() method:
// Task that return a String public class YahooQuote implements Callable<String>{
To pass the URL to the class we create a new constructor that receive the parameter:
private final String jsonURL; public YahooQuote(String jsonURL) { this.jsonURL = jsonURL; }
The result is stored in the String jsonString.
2. Parse JSON
Now we have our json result in a String. We can easily transform the String in a JSON object with the function … JSONObject:
Because of the nature of the answer (multiple level JSON objects) we have to create a new JSON object for each level
private String readPrice(String json) throws JSONException { JSONObject jsonObject = new JSONObject(json); JSONObject query = jsonObject.getJSONObject("query"); JSONObject results = query.getJSONObject("results"); JSONObject quote = results.getJSONObject("quote"); return quote.getString("LastTradePriceOnly"); }
We get the price result with getString();
Recommend
-
11
Angular HttpClient (6/7/8/9/10): Consume REST API Example by Didin J., updated on Jun 30, 2020
-
18
Friday, December 11, 2020 Use Service Reference in Visual Studio 2019 to consume a REST API with OpenAPI description from a C# console app In this tutorial I will show you h...
-
10
How to access org.apache.axis from the OSGI package in an XPage to consume a web service? advertisements I'm trying to consume a Web Service f...
-
4
Flutter Tutorial: Consume CRUD REST API Android and iOS Apps by Didin J., updated on Aug 29, 2020 The comprehensive step by step Flutter tutorial on building Android and iOS apps that consume the CRUD REST API
-
11
Lodyne Mark Posted on Mar 17...
-
10
ReactJS is a popular and widely used Javascript library for building rich user interfaces. this article shows how to consume Rest Services from a Quarkus application in a simple React front-end. Get...
-
20
Bruno Mulinari May 23, 2022 6 minute read
-
12
How to use the ABAP Client Proxy to consume a public REST service. This blog shows how the Client Proxy (CP) can be used to consume any public REST service. These features will be available from ABAP Platform 2022 and the...
-
18
PALLAB HALDAR January 6, 2023 1 minute read
-
8
Angular 9 HTTP POST Example with JS...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK