Search in sources :

Example 1 with GithubUserBean

use of home.smart.fly.httpurlconnectiondemo.retrofit2.bean.GithubUserBean in project My-MVP by REBOOTERS.

the class Retrofit2DemoActivity method SimpleRetrofit.

private void SimpleRetrofit() {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    Retrofit.Builder builder = new Retrofit.Builder().baseUrl(BASE_URL);
    Retrofit retrofit = builder.client(httpClient.build()).build();
    ApiService simpleService = retrofit.create(ApiService.class);
    Call<ResponseBody> call = simpleService.getUserString(name);
    call.enqueue(new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            loading.dismiss();
            try {
                String result = response.body().string();
                Gson gson = new Gson();
                GithubUserBean bean = gson.fromJson(result, GithubUserBean.class);
                setUserView(bean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            loading.dismiss();
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GithubUserBean(home.smart.fly.httpurlconnectiondemo.retrofit2.bean.GithubUserBean) Gson(com.google.gson.Gson) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit)

Example 2 with GithubUserBean

use of home.smart.fly.httpurlconnectiondemo.retrofit2.bean.GithubUserBean in project My-MVP by REBOOTERS.

the class Retrofit2DemoActivity method LazyRetrofit.

private void LazyRetrofit() {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    Retrofit.Builder builder = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create());
    Retrofit retrofit = builder.client(httpClient.build()).build();
    ApiService service = retrofit.create(ApiService.class);
    Call<GithubUserBean> call = service.getUser(name);
    call.enqueue(new Callback<GithubUserBean>() {

        @Override
        public void onResponse(Call<GithubUserBean> call, Response<GithubUserBean> response) {
            GithubUserBean bean = response.body();
            setUserView(bean);
            loading.dismiss();
        }

        @Override
        public void onFailure(Call<GithubUserBean> call, Throwable t) {
            loading.dismiss();
        }
    });
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GithubUserBean(home.smart.fly.httpurlconnectiondemo.retrofit2.bean.GithubUserBean)

Example 3 with GithubUserBean

use of home.smart.fly.httpurlconnectiondemo.retrofit2.bean.GithubUserBean in project My-MVP by REBOOTERS.

the class Retrofit2DemoActivity method RxRetrofit.

private void RxRetrofit() {
    ApiService service = GenServiceUtil.createService(ApiService.class);
    final Call<GithubUserBean> call = service.getUser(name);
    final Observable myObserable = Observable.create(new Observable.OnSubscribe<GithubUserBean>() {

        @Override
        public void call(Subscriber<? super GithubUserBean> subscriber) {
            Response<GithubUserBean> bean = null;
            try {
                bean = call.execute();
                subscriber.onNext(bean.body());
            } catch (IOException e) {
                e.printStackTrace();
                subscriber.onError(e);
            }
            subscriber.onCompleted();
        }
    });
    myObserable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).map(new Func1<GithubUserBean, GithubUserBean>() {

        @Override
        public GithubUserBean call(GithubUserBean o) {
            if (TextUtils.isEmpty(o.getBio())) {
                o.setBio("nothing !");
            }
            return o;
        }
    }).subscribe(new Subscriber<GithubUserBean>() {

        @Override
        public void onCompleted() {
            loading.dismiss();
        }

        @Override
        public void onError(Throwable e) {
            loading.dismiss();
        }

        @Override
        public void onNext(GithubUserBean o) {
            setUserView(o);
        }
    });
}
Also used : Response(retrofit2.Response) GithubUserBean(home.smart.fly.httpurlconnectiondemo.retrofit2.bean.GithubUserBean) IOException(java.io.IOException) Func1(rx.functions.Func1) Observable(rx.Observable)

Aggregations

GithubUserBean (home.smart.fly.httpurlconnectiondemo.retrofit2.bean.GithubUserBean)3 IOException (java.io.IOException)2 OkHttpClient (okhttp3.OkHttpClient)2 Retrofit (retrofit2.Retrofit)2 Gson (com.google.gson.Gson)1 ResponseBody (okhttp3.ResponseBody)1 Response (retrofit2.Response)1 Observable (rx.Observable)1 Func1 (rx.functions.Func1)1