Anyone that want to capitalize on a android market! Anyone that wants to save money on hiring an developer! Web Designers who want to get started into the Mobile App Industry. App Developers that want to learn an easier way of making apps. Website owners who want to convert responsive website to app. Enjoy millions of the latest Android apps, games, music, movies, TV, books, magazines & more. Anytime, anywhere, across your devices. Login here to access the FUT Web App and manage your FIFA Ultimate Team (FUT) while you're away from your console or PC. Under 'Activity controls,' tap Web & App Activity. Turn Web & App Activity on or off. When Web & App Activity is on: You can check the box next to 'Include Chrome history and activity from websites and apps that use Google services.' When this box is checked, you can control whether app activity from your device is saved.
A Web API is an online 'application programming interface' that allows developers to interact with external services. These are the commands that the developer of the service has determined will be used to access certain features of their program. It is referred to as an interface because a good API should have commands that make it intuitive to interact with.
An example of this might be if we want to get information about a user from their social media account. That social media platform would likely have a web API for developers to use in order to request that data. Other commonly used APIs handle things like advertising (AdMob), machine learning (ML Kit), and cloud storage.
It's easy to see how interacting with these types of services could extend the functionality of an app. In fact, the vast majority of successful apps on the Play Store will use at least one web API!
In this post, we'll explore how to use a web API from within an Android app.
How a Web API works
Most APIs work using either XML or JSON. These languages allow us to send and retrieve large amounts of useful information in the form of objects.
XML is eXtensible Markup Language. If you are an Android developer, then you're probably already familiar with XML from building your layouts and saving variables.
XML is easy to understand and generally places keys inside triangle brackets, followed by their values. It looks a bit like HTML:
JSON, on the other hand, stands for 'Javascript Object Notation.' It is a short-hand for sending data online. Like XML or a CSV file, it can be used to send 'value/attribute pairs.'
Here the syntax looks a little different, though:
These are 'data objects' in that they are conceptual entities (people in this case) that can be described by key/value pairs. We use these in our Android apps by turning them into objects just as we normally would, with the use of classes.
See also:How to use classes in Java
To see this in action, we need to find a Web API that we can use readily. In this example, we will be using JSON Placeholder. This is a free REST API specifically for testing and prototyping, which is perfect for learning a new skill! REST is a particular architectural 'style' that has become standard for communicating across networks. REST-compliant systems are referred to as 'RESTful' and share certain characteristics. You don't need to worry about that right now, however.
Setting up our project for Retrofit 2
For this example, we'll also be using something called Retrofit 2. Retrofit 2 is an extremely useful HTTP client for Android that allows apps to connect to a Web API safely and with a lot less code on our part. This can then be used, for example, to show Tweets from Twitter, or to check the weather. It significantly reduces the amount of work we need to do to get that working.
See also: Consuming APIs: Getting started with Retrofit on Android
First up, we need to add internet permission to our Android Manifest file to make sure our app is allowed to go online. Here is what you need to include:
We also need to add a dependency if we are going to get Retrofit 2 to work in our app. So in your module-level build.gradle file add:
We also need something called Gson:
Gson is what is going to convert the JSON data into a Java object for us (a process called deserialization). We could do this manually, but using tools like this makes life much easier!
There are actually later versions of Retrofit that make a few changes. If you want to be up-to-the-moment, check out the official website.
Converting JSON to Java object
A 'Route' is a URL that represents an endpoint for the API. If we take a look at JSON Placeholder, you'll see we have options such as '/posts' and '/comments?postId=1'. Chances are you will have seen URLs like this yourself while browsing the web!
Click on /posts and you'll see a large amount of data in JSON format. This is a dummy text that mimics the way a page full of posts on social media looks. It is the information we want to get from our app and then display on the screen.
To handle this information, we're going to need a class that can build objects from the deserialized data. To that end, create a new class in your project and call it 'PlaceholderPost'. Cobook 1 0 – an intelligent address book. Gemini 2 5 0 m. This will need variables that correspond to the data we're getting from the /posts page ('body', 'ID' etc.). We'll be getting that information from the web API, so we need a getter for each of them.
The final class should look like this:
This could just as easily be users on Twitter, messages on Facebook, or information about the weather!
Interface files
Next, we need a new interface file. You create this the same way you create a class: by clicking on your package name in the project window and choosing 'New > Class' but here you're selecting 'Interface' underneath where you enter the name. An interface file contains methods that are later implemented by a class. I've called mine 'PlaceholderAPI'.
This interface needs just a single method to retrieve all the data from '/Post'. If you take a look at that JSON again, you'll notice that the curly brackets are inside square brackets. This means that we have an array of objects, which is why we want to build a list for them. The objects are instances of our 'PlaceholderPost' that we just made, so that's what we're putting in here!
For those that are very new to programming, remember that any red lines probably mean you haven't imported a class. Just click on the highlighted statement and press alt+return to do this automatically.
(I can't imagine anyone using this as an early programming lesson but you never know!)
Web App Into Android Apps
This looks like so:
Displaying the content
Now, hop back into your main activity. We could build a fancy layout for displaying all this data, but to keep things nice and simple, I'm just going to stick with the layout as it is.
To use Retrofit, we're going to need to create a new Retrofit object. We do this with the following lines of code:
As you can see, we're passing in the rest of the URL here. We then want to use our interface:
Now we just need to call the method! Because things have been too easy so far, Android does throw a little spanner in the works by preventing you from doing this on the main thread. The reason, of course, is that if the process takes too long, it will end up freezing the app! This is true when using any Web API. It makes sense, but it's not terribly convenient when we just want to make a tutorial. Fortunately, we don't need to create a second thread ourselves as Retrofit actually does all that for us.
We'll now get an onResponse and onFailure callback. onFailure is, of course, where we need to handle any errors.
onResponse does not mean that everything went smoothly, however. It simply means that there was a response; that the website exists. Should we get a 404 message, this would still be considered a 'response.' Thus, we need to check again if the process went smoothly with isSuccessful(), which checks to see that the HTTP code is not an error.
To keep things really simple, I'm going to display just one piece of data from one of the objects we've received. To achieve this, I renamed the textView in the layout file to give it the id 'text'. You can experiment with this yourself.
The full code looks like this:
Wrapping up
At this point, you should have a good idea of how a web API works and why you want one. Planet moolah slots online free. You would have also created your first app that uses a web API to do something potentially useful.
Of course, there are countless other web APIs, and each work in their own ways. Some will require additional SDKs to use or different libraries. Likewise, there are many other actions beyond the 'GET' request we demonstrated here. For example, you can use 'POST' in order to send data to the server, which is useful if you ever want your users to be able to post to social media from your apps.
The possibilities are endless once you combine the power and flexibility of Android with the huge resources available online.
While re-writing my game Zoggle (Blog post coming soon) I decided to use Cordova (PhoneGap) to create a new mobile application for the game.So without further ado, here is how to turn a website into a native mobile Android application in less than 5 minutes.
Step 0 - Prerequisite: Installing Cordova CLI, and the Android SDK tools
To install the Cordova CLI, you first need to install Node.js version 0.10+.It is imperative that the version is 0.10+ or you will be unhappy.http://nodejs.org/
Sibelius 8 3 0 download free. Next run this command to install the Cordova CLI:sudo npm install -g cordova
Courier 1 3 2 – share files images and more. Alright, now let's install the Android SDK toolshttp://developer.android.com/sdk/installing/index.html
Step 1 - Hello World application
Step 2 - Portrait mode only
Now let's edit our android manifest to force the app to stay in portrait mode, edit:
and add this config to it (on ):
Step 3 - Content
Finally we get to adding our website. Edit your index.html to look similar to mine:
Step 3 - Icons
Lastly we need to add icons for our application.
You will find all icons here:
Just replace them with your icon (of the correct size).
And that's it. Now lets look into compiling for release on the app store.
Step 4 - Publishing!
Now we can generate a release version of the APK
Your APK file should be located here:
To submit it to the app store, we need to sign it (cryptographically). This page details how to do that:http://developer.android.com/tools/publishing/app-signing.htmlbut the short version is this:
And that's it! You can now upload that APK to Google play and publish your application.
Bonus - Splash Screen
I created a splash screen for Zoggle, but the game loaded so quickly that it became unnecessary. However it was a bit tricky, so I'll go ahead and explain the process.
First install the cordova plugin
Then edit your main activity Java file. Mine was here:
And add this line:
Then, in the app code above (www/index.html), uncomment the following line:
which will hide the splashscreen once the app loads.
Creating a splash screen image
Web App Into Android App Store
Splash screen images are not regular images, instead they are 9-patch images. This allows them to stretch to meet different screen sizes.Here is a great video introduction to how they work:
http://www.youtube.com/watch?v=MQRA9nwxU7gThe Android SDK tools come with a tool called draw9patch, which you can read about here:http://developer.android.com/tools/help/draw9patch.html
It's not the easiest to use, but it works. Simply run it, open your image, and zoom in on an edge of your image. The 1 pixel transparent border on your image will be the guide which lets the device know how to stretch the image.
Here is mine:
The black lines mean the the content there WILL BE STRETCHED (so long to figure this out.). This means if you don't want your logo to be distorted, draw the sections around the outside of your logo.Lastly, make sure your image ends in '.9.png' to let the device know that it is a 9-patch image. Then simply put it inside the folder next to your icon:
Done!Now go check out Zoggle!