栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Retrofit2在POST请求中发送JSON

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Retrofit2在POST请求中发送JSON

在gradle中使用这些

compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'com.squareup.retrofit2:converter-gson:2.3.0'compile 'com.squareup.retrofit2:converter-scalars:2.3.0'

使用这两个POJO类........

LoginData.class

public class LoginData {    private String email;    private String password;    public LoginData(String email, String password) {        this.email = email;        this.password = password;    }        public String getEmail() {        return email;    }        public void setEmail(String email) {        this.email = email;    }        public String getPassword() {        return 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;        public Boolean getError() {        return error;    }        public void setError(Boolean error) {        this.error = error;    }        public String getMessage() {        return message;    }        public void setMessage(String message) {        this.message = message;    }        public Integer getDoctorid() {        return doctorid;    }        public void setDoctorid(Integer doctorid) {        this.doctorid = doctorid;    }        public Boolean getActive() {        return active;    }        public void setActive(Boolean active) {        this.active = active;    }}

像这样使用API

public interface RetrofitInterface {     @POST("User/DoctorLogin")        Call<LoginResult> getStringScalar(@Body LoginData body);}

使用这样的呼叫....

Retrofit retrofit = new Retrofit.Builder() .baseUrl("Your domain URL here") .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build();       RetrofitInterface service = retrofit.create(RetrofitInterface .class); Call<LoginResult> call=service.getStringScalar(new LoginData(email,password));    call.enqueue(new Callback<LoginResult>() {     @Override     public void onResponse(Call<LoginResult> call, Response<LoginResult> 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<LoginResult> call, Throwable t) {//for getting error in network put here Toast, so get the error on network      } });

编辑:-

把它放在

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数据解析。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号