简体   繁体   中英

retrofit post request in body with raw data

url=url+/api/registerotp

I tried using this code but did not work correctly

post request in raw body data = {"mobile_or_email": "0123456789"}

response from server

{"status":true,"message":"Otp send Your Number!","otp":6425}

public interface Retrofit_request {
@POST("registerotp")
Call<model> getUser(@Body postd  mobile_or_email);}

public class model {

@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("otp")
@Expose
private Integer otp;

public Boolean getStatus() {
    return status;
}

public void setStatus(Boolean status) {
    this.status = status;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public Integer getOtp() {
    return otp;
}

public void setOtp(Integer otp) {
    this.otp = otp;
}}


 
public class RetrofitClient<minstance> {

private static final String BASE_URL=url+"/api/";
private static RetrofitClient minstance;
private Retrofit retrofit;
private RetrofitClient(){
    retrofit=new 
Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}
public static synchronized RetrofitClient getInstance(){
    if (minstance==null){
        minstance=new RetrofitClient();
    }
    return minstance;
}
public Retrofit_request getApi(){
    return retrofit.create(Retrofit_request.class);
}}

main class

    postd postd=new postd("8273217888");
    Call<model> call = RetrofitClient
            .getInstance()
            .getApi()
            .getUser(postd);
    call.enqueue(new Callback<model>() {
        @Override
        public void onResponse(Call<model> call, Response<model> response) {
            String s = null;
            s = response.message();
            Log.d("data", s);
            Log.d("data", "true"+response);

        }

        @Override
        public void onFailure(Call<model> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "" + t.getMessage(), Toast.LENGTH_SHORT).show();
            Log.d("data t", t.getMessage());
        }
    });`
public class postd {
@SerializedName("mobile_or_email")
String mobile_or_email;

public postd(String mobile_or_email) {
    this.mobile_or_email = mobile_or_email;
}

public String getMobile_or_email() {
    return mobile_or_email;
}

public void setMobile_or_email(String mobile_or_email) {
    this.mobile_or_email = mobile_or_email;
}

}

log-D/data: trueResponse{protocol=h2, code=200, message=, url=https://example.com/app2/api/registerotp}

I am new to retrofit so pls tell me how to fix it

public void onResponse(Call<model> call, Response<model> response) {
            model s = null;
            s = response.body();
            Log.d("data", s.getMessage()+""+s.getStatus()+" "+s.getOtp());

        }

Log:-D/data: Otp send Your Number!true 2208

Change s = response.message(); since response.message() is the Response's message, not the data returned niside the response.

Use response.body() instead.


This was discussed in the comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM