retrofit2 发送json数据_android – 使用Retrofit2在POST请求中发送JSON

Use these two POJO class ……..

LoginData.class

public class LoginData {

private String email;

private String password;

public LoginData(String email, String password) {

this.email = email;

this.password = password;

}

/**

*

* @return

* The email

*/

public String getEmail() {

return email;

}

/**

*

* @param email

* The email

*/

public void setEmail(String email) {

this.email = email;

}

/**

*

* @return

* The password

*/

public String getPassword() {

return password;

}

/**

*

* @param password

* The password

*/

public void setPassword(String password) {

this.password = password;

}

}

LoginResult.class

public class LoginResult {

private Boolean error;

private String message;

private Integer doctorid;

private Boolean active;

/**

*

* @return

* The error

*/

public Boolean getError() {

return error;

}

/**

*

* @param error

* The error

*/

public void setError(Boolean error) {

this.error = error;

}

/**

*

* @return

* The message

*/

public String getMessage() {

return message;

}

/**

*

* @param message

* The message

*/

public void setMessage(String message) {

this.message = message;

}

/**

*

* @return

* The doctorid

*/

public Integer getDoctorid() {

return doctorid;

}

/**

*

* @param doctorid

* The doctorid

*/

public void setDoctorid(Integer doctorid) {

this.doctorid = doctorid;

}

/**

*

* @return

* The active

*/

public Boolean getActive() {

return active;

}

/**

*

* @param active

* The active

*/

public void setActive(Boolean active) {

this.active = active;

}

}

Use API like this ….

@POST("User/DoctorLogin")

Call getStringScalar(@Body LoginData body);

use call like this ….

Call call=service.getStringScalar(new LoginData(email,password));

call.enqueue(new Callback() {

@Override

public void onResponse(Call call, Response response) {

//response.body() have your LoginResult fields and methods (example you have to access error then try like this response.body().getError() )

}

@Override

public void onFailure(Call call, Throwable t) {

//for getting error in network put here Toast, so get the error on network

}

});

编辑:-

put this inside the success() ….

if(response.body().getError()){

Toast.makeText(getBaseContext(),response.body().getMessage(),Toast.LENGTH_SHORT).show();

}else {

//response.body() have your LoginResult fields and methods (example you have to access error then try like this response.body().getError() )

String msg = response.body().getMessage();

int docId = response.body().getDoctorid();

boolean error = response.body().getError();

boolean activie = response.body().getActive()();

}

注意: – 始终使用POJO类,它会在改进中删除JSON数据解析.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值