Skip to content

Instantly share code, notes, and snippets.

@SergejIsbrecht
Created October 26, 2016 19:58
Show Gist options
  • Save SergejIsbrecht/fa6f14c85962d35e241599d60942b586 to your computer and use it in GitHub Desktop.
Save SergejIsbrecht/fa6f14c85962d35e241599d60942b586 to your computer and use it in GitHub Desktop.
package com.example.hanlufeng.retrofitdemo;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Url;
interface ApiEndpoints {
@GET
Call<ResponseBody> rxGetImageCall(@Url String imageUrl);
}
package com.example.hanlufeng.retrofitdemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
public class MainActivity extends Activity {
OkHttpClient client;
Retrofit retrofit;
ApiEndpoints apiService;
final String url = "http://javascript.info/tutorial/hello-world";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
//logging.setLevel(HttpLoggingInterceptor.Level.BODY);
Gson gson = new GsonBuilder()
.setLenient()
.create();
this.client = new OkHttpClient.Builder()
//.addInterceptor(logging)
.connectTimeout(25, TimeUnit.SECONDS)
.readTimeout(25, TimeUnit.SECONDS)
.build();
// retrofit with custom client
this.retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
//.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
this.apiService = retrofit.create(ApiEndpoints.class);
Thread mThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Call<ResponseBody> downCall = downloadImageCall(url);
Response<ResponseBody> response = downCall.execute();
String value = String.format("body: %1$s --- successful: %2$s --- message: %3$s --- raw: %4$s", response.body().string(),
response.isSuccessful(), response.message(), response.raw().toString()
);
Log.i("Separate", "*********************************************************************");
Log.i("VALUE", value);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
mThread.start();
}
public Call<ResponseBody> downloadImageCall(String uri) {
return apiService.rxGetImageCall(uri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment