Created
October 27, 2016 16:37
-
-
Save SergejIsbrecht/ed9bb60e0d0f8799bf9caa98b5df8950 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.MediaType; | |
import okhttp3.OkHttpClient; | |
import okhttp3.RequestBody; | |
import okhttp3.ResponseBody; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit2.Converter; | |
import retrofit2.Retrofit; | |
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
import rx.Observable; | |
import rx.android.schedulers.AndroidSchedulers; | |
import rx.functions.Action1; | |
import rx.functions.Func0; | |
import rx.functions.Func1; | |
import rx.schedulers.Schedulers; | |
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(); | |
logging.setLevel(HttpLoggingInterceptor.Level.BODY); | |
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(new ToStringConverterFactory()) | |
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | |
.client(client) | |
.build(); | |
this.apiService = retrofit.create(ApiEndpoints.class); | |
Observable<String> responseBodyObservable = apiService.rxGetImageCall(url).subscribeOn(Schedulers.io()); | |
responseBodyObservable.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Action1<String>() { | |
@Override | |
public void call(String s) { | |
Log.i("TEST", "--------------------------------"); | |
Log.i("RESULT", s); | |
} | |
}, new Action1<Throwable>() { | |
@Override | |
public void call(Throwable throwable) { | |
Log.i("TEST", "--------------------------------"); | |
Log.i("ERROR", throwable.toString()); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.hanlufeng.retrofitdemo; | |
import retrofit2.http.GET; | |
import retrofit2.http.Url; | |
import rx.Observable; | |
interface ApiEndpoints { | |
@GET | |
Observable<String> rxGetImageCall(@Url String imageUrl); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## from https://github.com/kamilwlf/Kamil-thoughts/blob/master/ToStringConverterFactory.java | |
package com.example.hanlufeng.retrofitdemo; | |
import java.io.IOException; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import okhttp3.ResponseBody; | |
import retrofit2.Converter; | |
import retrofit2.Retrofit; | |
class ToStringConverterFactory extends Converter.Factory { | |
@Override | |
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { | |
if (String.class.equals(type)) { | |
return new Converter<ResponseBody, String>() { | |
@Override | |
public String convert(ResponseBody value) throws IOException { | |
return value.string(); | |
} | |
}; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment