android学习杂记.5

来源:互联网 发布:js只能输入数字小数点 编辑:程序博客网 时间:2024/05/16 04:38

简单的rxjava+retrofit2的例子


配置retrofit

public class RetrofitClient {    private  static RetrofitService retrofitService;    public  static RetrofitService getClient(){        if(retrofitService==null){            OkHttpClient.Builder builder = new OkHttpClient.Builder();            builder.connectTimeout(5, TimeUnit.SECONDS);            builder.readTimeout(5, TimeUnit.SECONDS);//            OkHttpClient okHttpClient=new OkHttpClient();            Retrofit client=new Retrofit.Builder()                    .baseUrl(Urls.BASE_URL)                    .client(builder.build())                    .addConverterFactory(GsonConverterFactory.create())                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())                    .build();            retrofitService=client.create(RetrofitService.class);        }        return retrofitService;    }}

访问的接口地址

public interface RetrofitService {  @GET("index.php/api/event_cal?id=1")  Call<MeetingClass> getMeetingLists();  @POST("/MyApp/{link}")  Observable<String> login(@Path("link")String link, @Query("username")String username, @Query("psw")String psw);   @GET("/MyApp/{link}")  Observable<List<Users>> getAllUsers(@Path("link") String link);   @GET("/MyApp/{link}")  Observable<List<DisplayGoods>> getDisplayGoods(@Path("link") String link);
}

public void login(final String username, String psw, SharedPreferences sharedPreferences) {    final SharedPreferences.Editor editor=sharedPreferences.edit();    RetrofitService retrofitService= RetrofitClient.getClient();    Observable<String> observable = retrofitService.login("login",username.toString(),psw);    observable.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())            .subscribe(new Subscriber<String>() {                @Override                public void onCompleted() {                }                @Override                public void onError(Throwable e) {                    iLogin.isLogin(false);                }                @Override                public void onNext(String s) {                    if("true".equals(s)) {                        iLogin.isLogin(true);                        editor.putString("userName",username);                        editor.commit();                    }                    else                        iLogin.isLogin(false);                }            });}

要导入的包

compile 'com.squareup.retrofit2:retrofit:2.1.0'compile 'com.squareup.okhttp3:okhttp:3.6.0'compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'io.reactivex:rxjava:1.2.6'compile 'io.reactivex:rxandroid:1.2.1'compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

下面是没加rxjava的时候


配置retrofit

public class RetrofitExpClient {    private  static RetroExpService retrofitService;    public  static RetroExpService getClient(){        if(retrofitService==null){            OkHttpClient okHttpClient=new OkHttpClient();            Retrofit client=new Retrofit.Builder()                    .baseUrl(ConstantData.BASE_URL)                    .client(okHttpClient)                    .addConverterFactory(GsonConverterFactory.create())                    .build();            retrofitService=client.create(RetroExpService.class);        }        return retrofitService;    }}

接口地址

public interface RetroExpService {  //获取用户电话和email;  @GET("/hpproject/{link}")  Call<User> getUserInfo(@Path("link") String link, @Query("id") int id);    //获取所有歌曲    @GET("/hpproject/{link}")    Call<List<Music>> getMusic(@Path("link") String link);   //获取所有帖子    @GET("/hpproject/{link}")    Call<List<Post>> getPost(@Path("link") String link, @Query("position") String position);  //上传帖子//回帖    @Multipart    @POST("/hpproject/{link}")    Call<String> upFile(@Path("link") String link, @Part("photo") RequestBody photo, @QueryMap Map<String,String> map);

}


方法

public void getPostTopPic(){    RetroExpService retroExpService = RetrofitExpClient.getClient();    Call<List<String>> call = retroExpService.getPostTopPic("getPostTopPic");    call.enqueue(new Callback<List<String>>() {        @Override        public void onResponse(Call<List<String>> call, Response<List<String>> response) {            if (response.isSuccessful()) {                          }        }        @Override        public void onFailure(Call<List<String>> call, Throwable t) {        }    });}




0 0