GithubHelp home page GithubHelp logo

codepath / android-rest-client-template Goto Github PK

View Code? Open in Web Editor NEW
328.0 45.0 172.0 3.12 MB

Template Creating an Android OAuth REST Client

Home Page: https://github.com/codepath/android-rest-client-template

License: MIT License

Java 100.00%

android-rest-client-template's Introduction

RestClientTemplate Build Status

Overview

RestClientTemplate is a skeleton Android project that makes writing Android apps sourced from OAuth JSON REST APIs as easy as possible. This skeleton project combines the best libraries and structure to enable quick development of rich API clients. The following things are supported out of the box:

  • Authenticating with any OAuth 1.0a or OAuth 2 API
  • Sending requests for and parsing JSON API data using a defined client
  • Persisting data to a local SQLite store through an ORM layer
  • Displaying and caching remote image data into views

The following libraries are used to make this possible:

  • scribe-java - Simple OAuth library for handling the authentication flow.
  • Android Async HTTP - Simple asynchronous HTTP requests with JSON parsing
  • codepath-oauth - Custom-built library for managing OAuth authentication and signing of requests
  • Glide - Used for async image loading and caching them in memory and on disk.
  • Room - Simple ORM for persisting a local SQLite database on the Android device

Usage

1. Configure the REST client

Open src/com.codepath.apps.restclienttemplate/RestClient.java. Configure the REST_API_INSTANCE andREST_URL.

For example if I wanted to connect to Twitter:

// RestClient.java
public class RestClient extends OAuthBaseClient {
    public static final BaseApi REST_API_INSTANCE = TwitterApi.instance();
    public static final String REST_URL = "https://api.twitter.com/1.1";
    public static final String REST_CONSUMER_KEY = BuildConfig.CONSUMER_KEY;       // Change this inside apikey.properties
    public static final String REST_CONSUMER_SECRET = BuildConfig.CONSUMER_SECRET; // Change this inside apikey.properties
    // ...constructor and endpoints
}

Rename the apikey.properties.example file to apikey.properties. Replace the CONSUMER_KEY and CONSUMER_SECRET to the values specified in the Twitter console:

CONSUMER_KEY="adsflfajsdlfdsajlafdsjl" CONSUMER_SECRET="afdsljkasdflkjsd"

Next, change the intent_scheme and intent_host in strings.xml to a unique name that is special for this application. This is used for the OAuth authentication flow for launching the app through web pages through an Android intent.

<string name="intent_scheme">oauth</string>
<string name="intent_host">codepathtweets</string>

Next, you want to define the endpoints which you want to retrieve data from or send data to within your client:

// RestClient.java
public void getHomeTimeline(int page, JsonHttpResponseHandler handler) {
  String apiUrl = getApiUrl("statuses/home_timeline.json");
  RequestParams params = new RequestParams();
  params.put("page", String.valueOf(page));
  getClient().get(apiUrl, params, handler);
}

Note we are using getApiUrl to get the full URL from the relative fragment and RequestParams to control the request parameters. You can easily send post requests (or put or delete) using a similar approach:

// RestClient.java
public void postTweet(String body, JsonHttpResponseHandler handler) {
    String apiUrl = getApiUrl("statuses/update.json");
    RequestParams params = new RequestParams();
    params.put("status", body);
    getClient().post(apiUrl, params, handler);
}

These endpoint methods will automatically execute asynchronous requests signed with the authenticated access token. To use JSON endpoints, simply invoke the method with a JsonHttpResponseHandler handler:

// SomeActivity.java
RestClient client = RestApplication.getRestClient();
client.getHomeTimeline(1, new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Headers headers, JSON json) {
    // json.jsonArray.getJSONObject(0).getLong("id");
  }
});

Based on the JSON response (array or object), you need to declare the expected type inside the OnSuccess signature i.e public void onSuccess(JSONObject json). If the endpoint does not return JSON, then you can use the AsyncHttpResponseHandler:

RestClient client = RestApplication.getRestClient();
client.getSomething(new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Headers headers, String response) {
        System.out.println(response);
    }
});

Check out Android Async HTTP Docs for more request creation details.

2. Define the Models

In the src/com.codepath.apps.restclienttemplate.models, create the models that represent the key data to be parsed and persisted within your application.

For example, if you were connecting to Twitter, you would want a Tweet model as follows:

// models/Tweet.java
package com.codepath.apps.restclienttemplate.models;

import androidx.room.ColumnInfo;
import androidx.room.Embedded;

import androidx.room.Entity;
import androidx.room.PrimaryKey;

import org.json.JSONException;
import org.json.JSONObject;

@Entity
public class Tweet {
  // Define database columns and associated fields
  @PrimaryKey
  @ColumnInfo
  Long id;
  @ColumnInfo
  String userHandle;
  @ColumnInfo
  String timestamp;
  @ColumnInfo
  String body;

  // Use @Embedded to keep the column entries as part of the same table while still
  // keeping the logical separation between the two objects.
  @Embedded
  User user;
}

Note there is a separate User object but it will not actually be declared as a separate table. By using the @Embedded annotation, the fields in this class will be stored as part of the Tweet table. Room specifically does not load references between two different entities for performance reasons (see https://developer.android.com/training/data-storage/room/referencing-data), so declaring it this way causes the data to be denormalized as one table.

// models/User.java

public class User {

    @ColumnInfo
    String name;

    // normally this field would be annotated @PrimaryKey because this is an embedded object
    // it is not needed
    @ColumnInfo  
    Long twitter_id;
}

Notice here we specify the SQLite table for a resource, the columns for that table, and a constructor for turning the JSON object fetched from the API into this object. For more information on creating a model, check out the Room guide.

In addition, we also add functions into the model to support parsing JSON attributes in order to instantiate the model based on API data. For the User object, the parsing logic would be:

// Parse model from JSON
public static User parseJSON(JSONObject tweetJson) {

    User user = new User();
    this.twitter_id = tweetJson.getLong("id");
    this.name = tweetJson.getString("name");
    return user;
}

For the Tweet object, the logic would would be:

// models/Tweet.java
@Entity
public class Tweet {
  // ...existing code from above...

  // Add a constructor that creates an object from the JSON response
  public Tweet(JSONObject object){
    try {
      this.user = User.parseJSON(object.getJSONObject("user"));
      this.userHandle = object.getString("user_username");
      this.timestamp = object.getString("timestamp");
      this.body = object.getString("body");
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  public static ArrayList<Tweet> fromJson(JSONArray jsonArray) {
    ArrayList<Tweet> tweets = new ArrayList<Tweet>(jsonArray.length());

    for (int i=0; i < jsonArray.length(); i++) {
        JSONObject tweetJson = null;
        try {
            tweetJson = jsonArray.getJSONObject(i);
        } catch (Exception e) {
            e.printStackTrace();
            continue;
        }

        Tweet tweet = new Tweet(tweetJson);
        tweets.add(tweet);
    }

    return tweets;
  }
}

Now you have a model that supports proper creation based on JSON. Create models for all the resources necessary for your mobile client.

4. Define your queries

Next, you will need to define the queries by creating a Data Access Object (DAO) class. Here is an example of declaring queries to return a Tweet by the post ID, retrieve the most recent tweets, and insert tweets.

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;

import java.util.List;

@Dao
public interface TwitterDao {
    // Record finders
    @Query("SELECT * FROM Tweet WHERE post_id = :tweetId")
    Tweet byTweetId(Long tweetId);

    @Query("SELECT * FROM Tweet ORDER BY created_at")
    List<Tweet> getRecentTweets();

    // Replace strategy is needed to ensure an update on the table row.  Otherwise the insertion will
    // fail.
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertTweet(Tweet... tweets);
}

The examples here show how to perform basic queries on the Tweet table. If you need to declare one-to-many or many-to-many relations, see the guides on using the @Relation and @ForeignKey annotations.

5. Create database

We need to define a database that extends RoomDatabase and describe which entities as part of this database. We also need to include what data access objects are to be included. If the entities are modified or additional ones are included, the version number will need to be changed. Note that only the Tweet class is declared:

// bump version number if your schema changes
@Database(entities={Tweet.class}, version=1)
public abstract class MyDatabase extends RoomDatabase {
  // Declare your data access objects as abstract
  public abstract TwitterDao twitterDao();

  // Database name to be used
  public static final String NAME = "MyDataBase";

When compiling the code, the schemas will be stored in a schemas/ directory assuming this statement has been included your app/build.gradle file. These schemas should be checked into your code based.

android {
    defaultConfig {

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
    }

}

6. Initialize database

Inside your application class, you will need to initialize the database and specify a name for it.

public class RestClientApp extends Application {

  MyDatabase myDatabase;

  @Override
  public void onCreate() {
    // when upgrading versions, kill the original tables by using fallbackToDestructiveMigration()
    myDatabase = Room.databaseBuilder(this, MyDatabase.class, MyDatabase.NAME).fallbackToDestructiveMigration().build();
  }

  public MyDatabase getMyDatabase() {
    return myDatabase;
  }

}

7. Setup Your Authenticated Activities

Open src/com.codepath.apps.restclienttemplate/LoginActivity.java and configure the onLoginSuccess method which fires once your app has access to the authenticated API. Launch an activity and begin using your REST client:

// LoginActivity.java
@Override
public void onLoginSuccess() {
  Intent i = new Intent(this, TimelineActivity.class);
  startActivity(i);
}

In your new authenticated activity, you can access your client anywhere with:

RestClient client = RestApplication.getRestClient();
client.getHomeTimeline(1, new JsonHttpResponseHandler() {
  public void onSuccess(int statusCode, Headers headers, JSON json) {
    Log.d("DEBUG", "timeline: " + json.jsonArray.toString());
    // Load json array into model classes
  }
});

You can then load the data into your models from a JSONArray using:

ArrayList<Tweet> tweets = Tweet.fromJSON(jsonArray);

or load the data from a single JSONObject with:

Tweet t = new Tweet(json);
// t.body = "foo"

To save, you will need to perform the database operation on a separate thread by creating an AsyncTask and adding the item:

AsyncTask<Tweet, Void, Void> task = new AsyncTask<Tweet, Void, Void>() {
    @Override
    protected Void doInBackground(Tweet... tweets) {
      TwitterDao twitterDao = ((RestApplication) getApplicationContext()).getMyDatabase().twitterDao();
      twitterDao.insertModel(tweets);
      return null;
    };
  };
  task.execute(tweets);

That's all you need to get started. From here, hook up your activities and their behavior, adjust your models and add more REST endpoints.

Extras

Loading Images with Glide

If you want to load a remote image url into a particular ImageView, you can use Glide to do that with:

Glide.with(this).load(imageUrl)
     .into(imageView);

This will load an image into the specified ImageView and resize the image to fit.

Logging Out

You can log out by clearing the access token at any time through the client object:

RestClient client = RestApplication.getRestClient();
client.clearAccessToken();

Viewing SQL table

You can use chrome://inspect to view the SQL tables once the app is running on your emulator. See this guide for more details.

Adding OAuth2 support

Google uses OAuth2 APIs so make sure to use the GoogleApi20 instance:

public static final BaseApi REST_API_INSTANCE = GoogleApi20.instance();

Change REST_URL to use the Google API:

public static final String REST_URL = "https://www.googleapis.com/calendar/v3"; // Change this, base API URL

The consumer and secret keys should be retrieved via the credentials section in the Google developer console You will need to create an OAuth2 client ID and client secret.

Create a file called apikey.properties:

REST_CONSUMER_KEY="XXX-XXX.apps.googleusercontent.com"
REST_CONSUMER_SECRET="XX-XXXXXXX"

The OAuth2 scopes should be used according to the ones defined in the OAuth2 scopes:

public static final String OAUTH2_SCOPE = "https://www.googleapis.com/auth/calendar.readonly";

Make sure to pass this value into the scope parameter:

public RestClient(Context context) {
		super(context, REST_API_INSTANCE,
				REST_URL,
				REST_CONSUMER_KEY,
				REST_CONSUMER_SECRET,
				OAUTH2_SCOPE,  // OAuth2 scope, null for OAuth1
				String.format(REST_CALLBACK_URL_TEMPLATE, context.getString(R.string.intent_host),
						context.getString(R.string.intent_scheme), context.getPackageName(), FALLBACK_URL));
	}

Google only accepts http:// or https:// domains, so your REST_CALLBACK_URL_TEMPLATE will need to be adjusted:

public static final String REST_CALLBACK_URL_TEMPLATE = "https://localhost";

Make sure to update the cprest and intent_host to match this callback URL .

Troubleshooting

  • If you receive the following error org.scribe.exceptions.OAuthException: Cannot send unauthenticated requests for TwitterApi client. Please attach an access token! then check the following:
  • Is your intent-filter with <data> attached to the LoginActivity? If not, make sure that the LoginActivity receives the request after OAuth authorization.
  • Is the onLoginSuccess method being executed in the LoginActivity. On launch of your app, be sure to start the app on the LoginActivity so authentication routines execute on launch and take you to the authenticated activity.
  • If you are plan to test with Android API 24 or above, you will need to use Chrome to launch the OAuth flow.
  • Note that the emulators (both the Google-provided x86 and Genymotion versions) for API 24+ versions can introduce intermittent issues when initiating the OAuth flow for the first time. For best results, use an device for this project.

android-rest-client-template's People

Contributors

kapoor avatar nesquena avatar nickai avatar rogerhu avatar shisheng-1 avatar simonque avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-rest-client-template's Issues

New logo/icon proposal

Good day sir. I am a graphic designer and i am interested in designing a logo for your good project. I will be doing it as a gift for free. I just need your permission first before I begin my design. Hoping for your positive feedback. Thanks

Step by step tutorial.

It would be wonderful if you guys can create a step by step tutorial for this template like you did in RottenTomatoes tutorial.

Relational Models

Also it would be wonderful if guys can put together a tutorial for relational models.

Response body is incorrect, Can't extract token and secret from this:

I have added the FALLBACK_URL = "https://codepath.github.io/android-rest-client-template/success.html" in my App setting of Twitter Developer console.
I have also mentioned String REST_CALLBACK_URL_TEMPLATE = "intent://%s#Intent;action=android.intent.action.VIEW;scheme=%s;package=%s;S.browser_fallback_url=%s;end";
oauth //intent_scheme
codepathtweets //intent_host
in my code but I am getting this issueResponse body is incorrect. Can't extract token and secret from this: '<?xml version='1.0' encoding='UTF-8'?><errors><error code="415">Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings</error></errors>' .
However I am getting authorisation successful in browser.

When I am replacing the value of REST_CALLBACK_URL_TEMPLATE to " " blank I am able to authorise the App and not getting any exception but unable to navigate to App.

NullPointerException after clicking "yes, authorize" in the Flickr dialogue

Should the code still work?

I added my private key and secret from Flickr API and tried to test this. I am getting this Exception.

D/LoginActivity: in 'AsyncTask.execute:' run: starts
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
W/System.err:     at com.codepath.oauth.OAuthTokenClient.fetchAccessToken(OAuthTokenClient.java:93)
        at com.codepath.oauth.OAuthBaseClient.authorize(OAuthBaseClient.java:145)
        at com.codepath.oauth.OAuthLoginActionBarActivity.onResume(OAuthLoginActionBarActivity.java:39)
W/System.err:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456)
        at android.app.Activity.performResume(Activity.java:8135)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4434)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4476)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Uploading a photo using Flickr API

I don't know if this is the right place to ask, but I am trying to upload a photo using the Flickr API and I think I missed something.

According to the doc, I need to make a POST request to this url : https://up.flickr.com/services/upload/ and sending the photo.

So I did :

public void uploadPhotos(AsyncHttpResponseHandler handler, Bitmap photo)
    {
        String apiUrl ="https://up.flickr.com/services/upload/";
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        byte[] b = bos.toByteArray();
        RequestParams params = new RequestParams("photo=", b);
        client.post(apiUrl, params, handler);
    }

But I always get the response :
No photo specified

I think I am missing to set something but I can't find what...
Could you help me?
Thanks!

[Feature Request] Authenticator and AccountManager

This a wonderful client template.

I wish to add AccountManager/AbstractAuthenticator to this template.

How to begin? any guidance?

I had put together AndroidFrappeAuthenticator.

You can take a look into https://github.com/revant/AndroidFrappeAuthenticator if anything can be used into this template.

it is an OAuth 2 client for authorization grant - bearer_token (access_token/refresh_token) flow

It was primarily hacked up as a client Authenticator for frappe.io server.
You can Add Settings > Accounts > Frappe Authenticator account.

It initiates webview with the authorize endpoint. Gets bearer token using authorization code and stores it using AccountManager

Once Account is added, account manager always gives valid bearerToken.
It refreshes the access_token if required using the refresh_token and replaces old expired bearer_token with new valid one.

If token from server is revoked, it shows sign-in error in notification and asks the user to login again.

API Server

Is there a way to create a RestClient for your own API server where only a few endpoints are behind basic authentication and rest of the endpoints are open ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.