In the previous post I wore about Retrofit, how to use it and possible issues with SSL connection (and how to tackle them).

This post is dedicated to relatively new and exciting Android Studio plugin that generates Android REST client and appropriate Model of the JSON response. The plugin that I’ve been playing with is called Magnet rest2mobile and form what I’ve got chance to test it will be a serious threat to existing android REST client libs like Retrofit. You can download the tool and find installation screen-shot-type manual here. Even though, it can be installed on Eclipse (personally haven’t tried that, just going along with Magnet’s claim), it’s first and foremost created to run on Android Studio. What Rest2Mobile does, is that it turns actual REST requests and responses into source code (Objective-C, Android, and JavaScript) that you can call from your mobile app. The source code automatically handles: – server connections – marshalling and unmarshalling JSON data – JSON data conversion into the native object types (no need to use POJO generators for the model/response object, classes are auto generated) Given REST request/response examples, rest2mobile can infer the entire object model for your requests and responses. The IDE plugins and CLI just ask you to specify the GET/POST/PUT/DELETE REST requests and responses and automatically generates the method and types required to invoke the REST service from your Mobile App.

So, lets take this to the test. I chose to use Reddit feed (in JSON format), mainly because of its complexity- if the tool can handle this, most likely will be able to handle anything. In the images below is the Reddit feed that can be accessed via http://www.reddit.com/.json.

If you want to access particular subreddit feed, you do it by http://www.reddit.com/r/[subreddit]/.json (1). It’s more than likely that you’ll need more than the first 25 results that will be fetched so if you want to scroll through the pages the call should be modified in the following format: httр://reddit.com/r/[subreddit]/.[rѕѕ/jѕon] ? limit=[limit]&after=[after] (2). For the purpose of simplicity I’ll present an example that utilizes the format (1). Needless to say, the first thing you need to do is to install the plugin (link provided in the first section of this blog post). After you do that, form the Studio’s status bar you choose freshly installed R2M plugin and you click on ‘Add REST API’. As a result of that action you’re presented with the following window:

Populate the necessary fields, generated Class name and package to which the class will belong to. Define the name of the method that will be created and also the API call, path and parameters (in the case of (2) this needs to be done). Click on ‘Test API’ to have a preview and if everything runs smoothly, press ‘Generate’. After this action is completed, the result is shown in the image below:

As it can be seen, I added another wrapper class for more control over the API call. Also, worth mentioning is that with this automated generation, unit test is also generated under src/androidtest/java/. The RedditAPI class is defined in the following manner:

public class RedditAPI { /* * give the context form where you want to call the method and the subbredit you want the feed from * */ public static void getSubRedditFeed(Context c, String subreddit) throws SchemaException { MagnetMobileClient magnetClient = MagnetMobileClient.getInstance(c); RedditMagnetAPIFactory controllerFactory = new RedditMagnetAPIFactory(magnetClient); RedditMagnetAPI redditMagnetAPI = controllerFactory.obtainInstance(); Call<SubredditFeedResult> callObject = redditMagnetAPI.getSubredditFeed(subreddit, null); //auto-generated object SubredditFeedResult result = null; try { result = callObject.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } //cache the result or manipulate it in meaningful manner // i.e. make a list of objects that have title, picURL and karma (Upvotes - Downvotes) /* for each children i: result.getData().getChildren().get(i).getData().getTitle(); result.getData().getChildren().get(i).getData().getUrl(); result.getData().getChildren().get(i).getData().getUps(); result.getData().getChildren().get(i).getData().getDowns(); */ result.getData(); } }

The method defined in the code above can be called in Activity/Fragment and is as simple as this:

public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); try { RedditAPI.getSubRedditFeed(this, "funny"); // "gonewild" optimal, though } catch (SchemaException e) { e.printStackTrace(); } } //.... }

The result form the call: And that’s it. As you can see it’s pretty simple to generate and handle API call. Highly usable and time saving tool. The cons maybe the (lack of) flexibility when it comes to making changes after a method (API call) is generated and error/exception handling (haven’t tested it with SSL connection).

All in all, it definitely looks like something I’ll use in future projects.

For more info see this video and this example.