Back to project page inbox-android.
The source code is released under:
MIT License
If you think the Android project inbox-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.inboxapp.androidsdk.scratchpad.scratchpad_apis; // w w w .j a v a 2s.c o m import android.util.Log; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import java.util.Collections; import java.util.Comparator; import java.util.List; import retrofit.RestAdapter; import retrofit.http.GET; import retrofit.http.Path; /** * Created by sylvianguessan on 8/5/14. */ public class TestApi { public TestApi() { } static class Contributor { String login; int contributions; } public String GET_with_OKHTPP() throws Exception { String ENDPOINT = "https://raw.github.com/square/okhttp/master/README.md"; OkHttpClient okHttpClient = new OkHttpClient(); // Create request for remote resource. Request request = new Request.Builder() .url(ENDPOINT) .build(); // Execute the request and retrieve the response. Response response = okHttpClient.newCall(request).execute(); Log.w("Test Api GET_with_OKHTPP()", response.body().string()); return "GET_with_OKHTPP()"; } public String GET_with_OKHTPP_with_GSON() throws Exception { String ENDPOINT = "https://api.github.com/repos/square/okhttp/contributors"; Gson GSON = new Gson(); TypeToken<List<Contributor>> CONTRIBUTORS_TYPETOKEN = new TypeToken<List<Contributor>>() {}; long start = System.currentTimeMillis(); OkHttpClient client = new OkHttpClient(); Request.Builder requestBuilder = new Request.Builder(); requestBuilder.url(ENDPOINT); requestBuilder.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"); Request request = requestBuilder.build(); // Deserialize HTTP response to concrete type Response response = client.newCall(request).execute(); //Reader reader = response.body().charStream(); // for some reason was failing List<Contributor> contributors = GSON.fromJson(response.body().string(), CONTRIBUTORS_TYPETOKEN.getType()); long end = System.currentTimeMillis(); Log.w("Okhttpwithgson Timing", "Time (ms): " + ((end - start))); Collections.sort(contributors, new Comparator<Contributor>() { @Override public int compare(Contributor contributor1, Contributor contributor2) { return contributor1.contributions - contributor2.contributions; //from smallest contribution to greatest } }); Log.w("body: ","contributor count"+ contributors.size() + " "+response.body().string()); for (Contributor contributor : contributors) { Log.w("Test GET_with_OKHTPP_with_GSON()", "contributor.login + "+": " + contributor.contributions); } return "GET_with_OKHTPP_with_GSON()"; } public String GET_with_OKHTPP_with_JackSON() throws Exception { String ENDPOINT = "https://api.github.com/repos/square/okhttp/contributors"; Gson GSON = new Gson(); long start = System.currentTimeMillis(); OkHttpClient client = new OkHttpClient(); Request.Builder requestBuilder = new Request.Builder(); requestBuilder.url(ENDPOINT); requestBuilder.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"); Request request = requestBuilder.build(); // Deserialize HTTP response to concrete type Response response = client.newCall(request).execute(); ObjectMapper mapper = new ObjectMapper(); List<JacksonContributor> jacksonContributors = mapper.readValue(response.body().string(), new TypeReference<List<JacksonContributor>>(){}); long end = System.currentTimeMillis(); Log.w("Okhttpwithjackson Timing", "Time (ms): " + ((end - start))); // Log.w("body: "," jackson contributor count"+ jacksonContributors.size() + " "+response.body().string()); Collections.sort(jacksonContributors, new Comparator<JacksonContributor>() { @Override public int compare(JacksonContributor contributor1, JacksonContributor contributor2) { return contributor1.contributions - contributor2.contributions; //from smallest contribution to greatest } }); for (JacksonContributor contributor : jacksonContributors) { Log.w("Test GET_with_OKHTPP_with_JackSON()", "contributor.login + "+": " + contributor.contributions); } return "GET_with_OKHTPP_with_JackSON()"; } interface GitHub { @GET("/repos/{owner}/{repo}/contributors") List<Contributor> contributors( @Path("owner") String owner, @Path("repo") String repo ); } public String GET_with_RETROFIT() { String API_URL = "https://api.github.com"; long start = System.currentTimeMillis(); RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(API_URL) .build(); // Create an instance of our GitHub API interface. GitHub github = restAdapter.create(GitHub.class); // Fetch and print a list of the contributors to this library. List<Contributor> contributors = github.contributors("square", "retrofit"); long end = System.currentTimeMillis(); Log.w("Retrofit Timing", "Time (ms): " + ((end - start))); for (Contributor contributor : contributors) { Log.w("GET_with_RETROFIT()", contributor.login + " (" + contributor.contributions + ")"); } return "GET_with_RETROFIT()"; } public void GET_with_AndroidNative() { } public void GET_with_AsyncHTTP() { } public void POST_with_RETROFIT() { } public void POST_with_OKHTPP() { } public void POST_with_AndroidNative() { } public void POST_with_AsyncHTTP() { } }