Android使用Retrofit2.0和RxJava2.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工程。

在AndroidManifest.xml添加

<uses-permission android:name="android.permission.INTERNET"/>

在build.gradle添加发下代码,使用Retrofit2.2.0, RxJava2.0.7, RxAndroid2.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.retrofit2demo D/MainActivity: test
03-16 21:14:15.313 312-312/cssweb.com.retrofit2demo D/MainActivity: 成功
03-16 21:14:15.313 312-312/cssweb.com.retrofit2demo D/MainActivity: response=test
03-16 21:14:15.313 312-312/cssweb.com.retrofit2demo D/MainActivity: 完成


完整工程源代码

https://github.com/chenhaifeng2016/Retrofit2Demo


你可能感兴趣的:(----Android,/,Kotlin,----Netty)