AndroidはRetrofit 2.0とRxJava 2.0を使ってネットワーク要求を処理します.


まずSpringBootを使って簡単なSpringMVCプロジェクトを作ります.
@RestController
public class RetrofitController {

    @RequestMapping("/users/{user}")
    public User getUser(@PathVariable("user") String u){
        System.out.println("user = " + u);

        User user = new User();
        user.setUsername("test");
        user.setEmail(("[email protected]"));
        return user;
    }
}
public class User {
    private String username;
    private String email;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
サーバーは8000ポートで動作します.
Android Studioを通じてAndroidプロジェクトを作成します.
Android Manifest.xmlに追加
<uses-permission android:name="android.permission.INTERNET"/>
build.gradleには、Retrofit 2..0、RxJava 2.0.7、RxAndroid 2.0、1、gson 2.8.0を使用して、コードを追加します.
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
//compile 'com.squareup.retrofit2:converter-jackson:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.google.code.gson:gson:2.8.0'

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.7'
UserServiceを定義する
public interface UserService {
    @GET("/users/{user}")
    Flowable getUser(@Path("user") String user);
}
テストコード
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //     ,        
        //      token  , cache  ,        

        tvResponse = (TextView) findViewById(R.id.tvResponse);
        btnRequest = (Button) findViewById(R.id.btnTest);

        btnRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "test");

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://10.0.0.16:8000/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .build();

                UserService userService = retrofit.create(UserService.class);
                userService.getUser("chenhaifeng2016")
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber() {
                            @Override
                            public void onSubscribe(Subscription s) {
                                s.request(Long.MAX_VALUE);
                            }

                            @Override
                            public void onNext(User user) {
                                Log.d(TAG, "  ");
                                Log.d(TAG, "response=" + user.getUsername());
                            }

                            @Override
                            public void onError(Throwable t) {
                                Log.d(TAG, "  ");
                            }

                            @Override
                            public void onComplete() {
                                Log.d(TAG, "  ");
                            }
                        });
            }
        });
実行効果
03-16 21:14:15.283 312-312/cssweb.com.retrofit t 2 demoD/MainActivity:test 03-16 21:14:15.313 3 3 312-312/cssweb.com.retrtrfit t 2 demoD/MainActivity:成功03-16 16:14:15.313 312 312-312-312/csswebbfidededededededededededededededededed d d d d d d d d d d d d d 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12/cssweshshshshshshshshshshshshshshshshshshshshshshshshshshaaaaaaaaaaaMainActivity:完了
完全なプロジェクトソースコード
https://github.com/chenhaifeng2016/Retrofit2Demo