Advertisement
yo2man

TreeHouse: Making our code asynchronous

Jul 6th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. Networking Asynchronous Get
  2. update
  3.  
  4. //Call back method = basically telling OkHttp to call us back when it is done with its work.
  5. // Enqueue() method = We are telling OkHttp to call us back
  6. // when OkHttp Library is done it calls us back with the:
  7. // onResponse() method
  8. // and if there is an error
  9. // onFailure() method
  10. // see image explanation:  
  11.  
  12. package com.teamtreehouse.stormy;
  13.  
  14. import android.support.v7.app.ActionBarActivity;
  15. import android.os.Bundle;
  16. import android.util.Log;
  17. import android.view.Menu;
  18. import android.view.MenuItem;
  19.  
  20. import com.squareup.okhttp.Call;
  21. import com.squareup.okhttp.Callback;
  22. import com.squareup.okhttp.OkHttpClient;
  23. import com.squareup.okhttp.Request;
  24. import com.squareup.okhttp.Response;
  25.  
  26. import java.io.IOException;
  27.  
  28.  
  29. public class MainActivity extends ActionBarActivity {
  30.  
  31.     public static final String TAG= MainActivity.class.getSimpleName();
  32.  
  33.     @Override
  34.     protected void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.activity_main);
  37.  
  38.         String apiKey = "d5faf0cb201f103df4dde0b8b0a4f490";
  39.         double latitude = 37.8267;
  40.         double longitude = -122.423;
  41.         String forecastUrl = "https://api.forecast.io/forecast/d5faf0cb201f103df4dde0b8b0a4f490/," + apiKey +
  42.                 "/" + latitude +"/" + "," + longitude;
  43.  
  44.         OkHttpClient client = new OkHttpClient();
  45.         Request request = new Request.Builder().url(forecastUrl).build();
  46.  
  47.  
  48.         Call call = client.newCall(request);
  49.         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.
  50.  
  51.             // This should be somewhat familiar at this point, once again we have an anonymous inner class.
  52.             // It's a callback type, and it has two methods that we need to override: on Failure and on Response.
  53.             @Override
  54.             public void onFailure(Request request, IOException e) {
  55.  
  56.             }
  57.  
  58.             @Override
  59.             public void onResponse(Response response) throws IOException {
  60.                 try {
  61.                     // Response response = call.execute(); <-execute is a synchronous method so we delete this line
  62.                     if (response.isSuccessful()){
  63.                         Log.v(TAG, response.body().string());
  64.                     }
  65.                 } catch (IOException e) {
  66.                     Log.e(TAG, "Exception caught: ", e);
  67.                 }
  68.  
  69.             }
  70.         });
  71.  
  72.         Log.d(TAG, "Main UI code is running!"); // log that code is running so we can tell it is running. simple.
  73.     }
  74. }
  75.  
  76.  
  77.  
  78.  
  79. //----------------------------------------------------------------------------------------------
  80. AndroidManifest.xml
  81.  
  82. // We haven't really talked about permissions yet.
  83. // But whenever we're doing something out of the ordinary in an Android app,
  84. // even connecting to the internet, we need to request special permission.
  85. // We do this in the Android manifest.
  86.  
  87. <?xml version="1.0" encoding="utf-8"?>
  88. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  89.     package="com.teamtreehouse.stormy" >
  90.  
  91.     <!--add this. It solves the 'java.lang.Security Exception: Permission denied (missing INTERNET permission)' by requesting permission to use the internet -->
  92.     <uses-permission android:name="android.permission.INTERNET" />
  93.  
  94.     <application
  95.         android:allowBackup="true"
  96.         android:icon="@mipmap/ic_launcher"
  97.         android:label="@string/app_name"
  98.         android:theme="@style/AppTheme" >
  99.         <activity
  100.             android:name=".MainActivity"
  101.             android:label="@string/app_name" >
  102.             <intent-filter>
  103.                 <action android:name="android.intent.action.MAIN" />
  104.  
  105.                 <category android:name="android.intent.category.LAUNCHER" />
  106.             </intent-filter>
  107.         </activity>
  108.     </application>
  109.  
  110. </manifest>
  111. //----------------------------------------------------------------------------------------------
  112.  
  113. // http://developer.android.com/guide/topics/security/permissions.html#permissions
  114. // 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