Skip to content

Instantly share code, notes, and snippets.

@briansalvattore
Last active November 1, 2016 00:07
Show Gist options
  • Save briansalvattore/b0854dc8b6418c9165eb9c7205978a57 to your computer and use it in GitHub Desktop.
Save briansalvattore/b0854dc8b6418c9165eb9c7205978a57 to your computer and use it in GitHub Desktop.
public class HelloWorld {
private static final int TIMEOUT = 120;
private static final String BASE = "https://randomuser.me/";
public static void main(String[] args) {
System.out.println("Hello");
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(TIMEOUT, TimeUnit.SECONDS)
.readTimeout(TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(logging)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
System.out.println("Init call");
retrofit.create(Service.class).getUser().enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> arg0, Response<JsonObject> arg1) {
System.out.println("Response");
System.out.println(arg1.body());
}
@Override
public void onFailure(Call<JsonObject> arg0, Throwable arg1) {
System.out.println("When call failure");
System.err.println(arg1.toString());
}
});
}
public interface Service {
@GET("/api")
Call<JsonObject> getUser();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment