Retrofit2 Post请求

第一步: 实体类

public class Bean {

public String code;
public String msg;
public data data;

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public Bean.data getData() {
    return data;
}

public void setData(Bean.data data) {
    this.data = data;
}

public class data {
    public String token;
    public String accountid;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getAccountid() {
        return accountid;
    }

    public void setAccountid(String accountid) {
        this.accountid = accountid;
    }
}

}

第二部:创建接口

  public interface IInfo {

  @FormUrlEncoded               //别丢了
  @POST("/zjvo/login")          //接口字段
  Call login(@Field("phonenum")String phoneNum,@Field("password")String password,@Field("ltype")String ltype);

}

第三部:请求操作

    String phoneNum = "%62%6a%53%38%61%48%45%39%33%66%50%46%6b%6c%32%39%65%55%66%41%4b%67%3d%3d";
    String password = "%37%45%2f%6c%5a%34%74%44%57%36%45%3d";
    String ltype = "1";

    Retrofit retrofit2 = new Retrofit.Builder()
            .baseUrl("http://zjyktio.zjdata.net")
            .addConverterFactory(GsonConverterFactory.create())
            .client(new OkHttpClient())
            .build();

    IInfo iInfo = retrofit2.create(IInfo.class);

    Call call = iInfo.login(phoneNum,password,ltype);

    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {

            Bean loginBean = response.body();

            Log.i("zzz","loginBean.getMsg(): " + loginBean.getMsg());

        }

        @Override
        public void onFailure(Call call, Throwable t) {
        }
    });

你可能感兴趣的:(Retrofit2 Post请求)