rxjava2.0+retrofit2.0初试水,小demo

一个简单的post异步。

1.添加依赖

compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'// 大神写的这个库可以支持到rxjava2.X
//compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'//retrofit现在只支持到rxjava1.XX
 
  
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile
'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

注意横线部分:

//(代码中:addCallAdapterFactory(RxJava2CallAdapterFactory.create())
 
  
 
  
 
  
//添加:compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'错误报异常:Error:
// com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/rxjava.properties

//   File1: C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\io.reactivex\rxjava\1.1.5\ece7b5d0870e66d8226dab6dcf47a2b12afff061\rxjava-1.1.5.jar
//   File2: C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\io.reactivex.rxjava2\rxjava\2.0.1\57f850a6b317e5582f1dbaff10a9e7d7e1fcdcfb\rxjava-2.0.1.jar 



代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import java.io.IOException;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

public class TestRetrofitUtil extends AppCompatActivity
{
   private static Retrofit retrofit = new Retrofit.Builder()//
         .baseUrl("url")//
         .addConverterFactory(GsonConverterFactory.create())//
         .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//不加或者参数类型不对,报异常: Could not locate call adapter for io.reactivex.Observable>
         .build();

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_test_retrofit_util);
      TianQiInterface tianQiImpl = retrofit.create(TianQiInterface.class);
      tianQiImpl.getTianqiP("860023810000118", "青岛")
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(new BaseObserver<Response<ResponseBody>>()
      {
         @Override
         public void onNext(Response<ResponseBody> response)
         {
            try
            {
               Log.e("resulttt", "=body=" + response.body().string());
            } catch (IOException e)
            {
               e.printStackTrace();
            }
         }
      });
   }

   /**
    * 创建API接口
    */
   public interface TianQiInterface
   {
      @GET("app/tianqiinfo/{imei}")
      Observable<Response<ResponseBody>> getTianqiG(@Query("imei") String imei, @Field("city") String imeidistrict);

      @FormUrlEncoded
      @POST("app/tianqiinfo")
      Observable<Response<ResponseBody>> getTianqiP(@Field("imei") String imei, @Field("city") String imeidistrict);
      // Call> getUser();
      // @FormUrlEncoded
      // @POST("book/reviews")
      // Call addReviews(@FieldMap Map fields);
   }

}



你可能感兴趣的:(Rxjava)