An Android App can connect to the internet to retrieve information.
API is “application programming interface.” When a website has an API, that means it has a way for other applications to connect to it to request data. This means you can get a website’s information through your own website or mobile app.
For now, this is an example that uses http://www.omdbapi.com/ to request movie information. (Suggestion: copy and paste it into a text editor to get rid of the text line wrapping)
Notes about the example:
- Calling the API has to be asynchronous, meaning that the request happens at the same time that the User Interface thread continues to look for input. If it wasn’t, it would cause the User Interface thread (UI thread) to freeze up, and Android doesn’t let you do that.
- The custom class GetMovieInfo extends the AsyncTask class. When a GetMovieInfo object is created and its execute() method is called, this starts the process of making the web request.
- HttpURLConnection class lets you create the request object that will talk to the outside API. You need to also create the URL (address) that you’re talking to. The parameters (such as movie title, in this case) are included in this URL in this example.
- When the response comes back, it will be in JSON format. Java has its own built in tools for reading JSON data.
- The response comes in as “Object o” inside the onPostExecute method. That Object o is interpreted as a JSON object, and certain pieces of data are extracted from it and displayed on the app’s views.
public class MainActivity extends AppCompatActivity { TextView status; TextView year; TextView plot; EditText title; Button submit; JSONObject data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); status = findViewById(R.id.status); title = findViewById(R.id.title); year = findViewById(R.id.year); plot = findViewById(R.id.plot); submit = findViewById(R.id.submit); } public void clicked(View v){ status.setText("Button was clicked."); new GetMovieInfo().execute(); } class GetMovieInfo extends AsyncTask { @Override protected Object doInBackground(Object[] objects) { try { URL url = new URL("http://www.omdbapi.com/?apikey=create_your_own_api_key&t="+t); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line).append("\n"); } bufferedReader.close(); return stringBuilder.toString(); } catch (IOException e) { e.printStackTrace(); return "error"; } } String t = title.getText().toString(); @Override protected void onPostExecute(Object o) { status.setText("Response: " + o.toString()); try { data = new JSONObject(o.toString()); String y = data.getString("Year"); year.setText(y); String p = data.getString("Plot"); plot.setText(p); } catch (JSONException e) { e.printStackTrace(); data = null; year.setText(""); plot.setText("(Movie not found)"); } } } }