原CSDN博客已弃用,文章会逐渐迁移过来。

接口文件

package com.administrator.testapp;



import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by Administrator on 2018/5/18.
 */

public interface Api {
@POST
@FormUrlEncoded
Call<String> get(@Field("111") String name);

//用户注册
@POST("account/register")
@FormUrlEncoded
Call<String> postRegister(@Field("phone") String phone,@Field("password") String password);

//用户登录
@POST("account/login")
@FormUrlEncoded
Observable<UserInfo> postLogin(@Field("phone") String phone, @Field("password") String password);
}

改Observable

RetrofitUtil,放开addCallAdapterFactory(RxJavaCallAdapterFactory.create())//rxjava转换器,这行注释

package com.administrator.testapp;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.GsonConverterFactory;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2018/5/18.
 */

public class RetrofitUtil {

private static Retrofit sRetrofit;
private static OkHttpClient mOkHttpClient;

private RetrofitUtil(){}

public static class Hide{
    static RetrofitUtil sRetrofitUtil = new RetrofitUtil();
}

public static RetrofitUtil getInstance() {
    return Hide.sRetrofitUtil;
}

public Api build(){
    if(mOkHttpClient == null){
        mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
    }

    if(sRetrofit == null){
        sRetrofit = new Retrofit.Builder()
                .baseUrl("http://116.196.111.10:8050/")
                .client(mOkHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create()) //数据转换器
                .addConverterFactory(GsonConverterFactory.create())//数据转换器
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//rxjava转换器
                .build();
    }
    return sRetrofit.create(Api.class);
}
}

外部调用

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.login_tv:
            RetrofitUtil.getInstance().build()
                    .postLogin(etPhoneLogin.getText().toString().trim(),etPwd.getText().toString().trim())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<UserInfo>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            Log.e("R","onError:"+e.toString());
                        }

                        @Override
                        public void onNext(UserInfo userInfo) {
                            MyApplication.setUserInfo(userInfo);
                                if (userInfo.getCode()==200) {
                                    Log.e("R",MyApplication.getUserInfo().getData().getPhone());
                                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                }else {
                                    Toast.makeText(LoginActivity.this,"您的账号密码有误,请重新输入",Toast.LENGTH_SHORT).show();
                                }
                        }
                    });
            break;
    }
}