Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Networking Asynchronous Get
- update
- //Call back method = basically telling OkHttp to call us back when it is done with its work.
- // Enqueue() method = We are telling OkHttp to call us back
- // when OkHttp Library is done it calls us back with the:
- // onResponse() method
- // and if there is an error
- // onFailure() method
- // see image explanation:
- package com.teamtreehouse.stormy;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import com.squareup.okhttp.Call;
- import com.squareup.okhttp.Callback;
- import com.squareup.okhttp.OkHttpClient;
- import com.squareup.okhttp.Request;
- import com.squareup.okhttp.Response;
- import java.io.IOException;
- public class MainActivity extends ActionBarActivity {
- public static final String TAG= MainActivity.class.getSimpleName();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- String apiKey = "d5faf0cb201f103df4dde0b8b0a4f490";
- double latitude = 37.8267;
- double longitude = -122.423;
- String forecastUrl = "https://api.forecast.io/forecast/d5faf0cb201f103df4dde0b8b0a4f490/," + apiKey +
- "/" + latitude +"/" + "," + longitude;
- OkHttpClient client = new OkHttpClient();
- Request request = new Request.Builder().url(forecastUrl).build();
- Call call = client.newCall(request);
- call.enqueue(new Callback() { // the enqueue method here executes the call, but it does so by putting it in a queue. It doesn't execute it right away, immediately. OkHttp has a queue that will execute asynchronous calls in the order they are added to the queue and it executes in the background. Simple.
- // This should be somewhat familiar at this point, once again we have an anonymous inner class.
- // It's a callback type, and it has two methods that we need to override: on Failure and on Response.
- @Override
- public void onFailure(Request request, IOException e) {
- }
- @Override
- public void onResponse(Response response) throws IOException {
- try {
- // Response response = call.execute(); <-execute is a synchronous method so we delete this line
- if (response.isSuccessful()){
- Log.v(TAG, response.body().string());
- }
- } catch (IOException e) {
- Log.e(TAG, "Exception caught: ", e);
- }
- }
- });
- Log.d(TAG, "Main UI code is running!"); // log that code is running so we can tell it is running. simple.
- }
- }
- //----------------------------------------------------------------------------------------------
- AndroidManifest.xml
- // We haven't really talked about permissions yet.
- // But whenever we're doing something out of the ordinary in an Android app,
- // even connecting to the internet, we need to request special permission.
- // We do this in the Android manifest.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.teamtreehouse.stormy" >
- <!--add this. It solves the 'java.lang.Security Exception: Permission denied (missing INTERNET permission)' by requesting permission to use the internet -->
- <uses-permission android:name="android.permission.INTERNET" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- //----------------------------------------------------------------------------------------------
- // http://developer.android.com/guide/topics/security/permissions.html#permissions
- // https://teamtreehouse.com/library/build-a-weather-app/concurrency-and-error-handling/making-our-code-asynchronous
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement