Advertisement
yo2man

Networking: getting third party API OkHTTP (synchronous get)

Jul 6th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. // Networking: getting third party API OkHTTP (synchronous get: https://github.com/square/okhttp/wiki/Recipes)
  2.  
  3.  
  4. MainActivity.java
  5.  
  6.  
  7.  
  8. package com.teamtreehouse.stormy;
  9.  
  10. import android.support.v7.app.ActionBarActivity;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15.  
  16. import com.squareup.okhttp.Call;
  17. import com.squareup.okhttp.OkHttpClient;
  18. import com.squareup.okhttp.Request;
  19. import com.squareup.okhttp.Response;
  20.  
  21. import java.io.IOException;
  22.  
  23.  
  24. public class MainActivity extends ActionBarActivity {
  25.  
  26.     public static final String TAG= MainActivity.class.getSimpleName();
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);
  32.  
  33.         String apiKey = "d5faf0cb201f103df4dde0b8b0a4f490";
  34.         double latitude = 37.8267;
  35.         double longitude = -122.423;
  36.         String forecastUrl = "https://api.forecast.io/forecast/d5faf0cb201f103df4dde0b8b0a4f490/," + apiKey +
  37.                 "/" + latitude +"/" + "," + longitude; //Concatenate the parameters
  38.  
  39.         //Recipe from OkHTTP github https://github.com/square/okhttp/wiki/Recipes
  40.         OkHttpClient client = new OkHttpClient(); // we want to build an HTTP request using OkHttp. Create a new 'OkHttpClient' variable and initialize it using the default constructor.
  41.  
  42.         // Build a request that the client will send to the server (Forecast.io). Simple:
  43.         Request request = new Request.Builder().url(forecastUrl).build();  // alt+enter then select import OkHTTP version of request //
  44.         /*   Can also be formatted this way (drop to the next line for readability
  45.         Request request = new Request.Builder()
  46.                                     .url(forecastUrl)
  47.                                     .build();
  48.          */
  49.  
  50.         /* example used in challenge:
  51.         Next we need to create and build a Request. Declare a new Request variable,
  52.         and then set it by chaining a few methods together.
  53.         First use new Request.Builder(), then chain the url() method using apiUrl as the parameter.
  54.         Then chain build() to finish.
  55.  
  56.                 Request request = new Request.Builder().url(apiUrl).build();
  57.  
  58.          */
  59.  
  60.         // And the next thing we need is a Call object, we're going to put this request inside a Call object.
  61.         // So, the data type is call, we'll name it call.
  62.         Call call = client.newCall(request);
  63.         // execute the call:
  64.         try {
  65.             //We have an execute method from our call class, and it returns a response object.
  66.             //So let's store that response in a variable:
  67.             Response response = call.execute();
  68.             //add this //now we want to check if the request is successful (there is a handy method from the response class):
  69.             if (response.isSuccessful()){
  70.             Log.v(TAG, response.body().string());//Log entire response body
  71.             }
  72.         } catch (IOException e) {
  73.             Log.e(TAG, "Exception caught: ", e);
  74.         }
  75.  
  76.  
  77.     }
  78. }
  79.  
  80.  
  81. // https://github.com/square/okhttp/wiki/Recipes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement