【问题标题】:Retrofit POST Method with WCF services getting error code 400 : Bad Request使用 WCF 服务改进 POST 方法得到错误代码 400:错误请求
【发布时间】:2018-11-04 12:34:40
【问题描述】:

我想在 Retrofit 中使用 JSON 数据调用 POST 方法(REST API)。 Postman 和 Volley 库运行良好,我想在 Retrofit 中实现它..

我这两天正在研究它,没有得到任何解决方案.. 我已经推荐了this linkthis link 以及更多看起来相似但对我来说不起作用的东西..可能是我做错了什么..

这些是我的输入,输出看起来像这样,我的编码部分就在这里

public class Api {
private static Retrofit retrofit = null;
public static ApiInterface getClient() {


    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl("xxxx/Service1.svc/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //Creating object for our interface
        ApiInterface api = retrofit.create(ApiInterface.class);
    return api; // return the APIInterface object
}}

和接口作为

public interface ApiInterface {

@FormUrlEncoded
@POST("UserLogin")
Call<SignUpResponse> registration(@Field("Mobile_no") String mobile,
                                  @Field("Password") String pass,
                                  @Field("RegID") String regId);}

并将 Pojo 类更新为

public class SignUpResponse {


@SerializedName("UserLoginResult")
@Expose
private UserLoginResult userLoginResult;

public UserLoginResult getUserLoginResult() {
    return userLoginResult;
}

public void setUserLoginResult(UserLoginResult userLoginResult) {
    this.userLoginResult = userLoginResult;
}}class UserLoginResult {

@SerializedName("Email_id")
@Expose
private String emailId;
@SerializedName("First_name")
@Expose
private String firstName;
@SerializedName("Last_name")
@Expose
private String lastName;
@SerializedName("Message")
@Expose
private String message;
@SerializedName("Mobile_no")
@Expose
private String mobileNo;
@SerializedName("Password")
@Expose
private String password;
@SerializedName("RegID")
@Expose
private String regID;
@SerializedName("Status")
@Expose
private String status;

public String getEmailId() {
    return emailId;
}

public void setEmailId(String emailId) {
    this.emailId = emailId;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getMessage() {
    return message;
}

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

public String getMobileNo() {
    return mobileNo;
}

public void setMobileNo(String mobileNo) {
    this.mobileNo = mobileNo;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getRegID() {
    return regID;
}

public void setRegID(String regID) {
    this.regID = regID;
}

public String getStatus() {
    return status;
}

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

在 MainActivity 上

Api.getClient().registration("xxxNumberhere","rd","222").enqueue(new Callback<SignUpResponse>() {
        @Override
        public void onResponse(Call<SignUpResponse> call, Response<SignUpResponse> response) {
            signUpResponsesData = response.body();
            Toast.makeText(getApplicationContext(), response.body().getUserLoginResult().getMessage(), Toast.LENGTH_SHORT).show();
            progressDialog.dismiss();

        }

        @Override
        public void onFailure(Call<SignUpResponse> call, Throwable t) {
            Log.d("response", t.getStackTrace().toString());
            progressDialog.dismiss();

        }
    });

我的依赖是

compile 'com.squareup.retrofit2:retrofit:2.1.0'
// JSON Parsing
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

提前谢谢..

【问题讨论】:

    标签: android json retrofit2


    【解决方案1】:

    以下类名和功能与我的项目有关

    你可以通过,

    public interface APIService {
        @POST("seq/restapi/checkpassword")
        @Headers({
            "Content-Type: application/json;charset=utf-8",
            "Accept: application/json;charset=utf-8",
            "Cache-Control: max-age=640000"
        })
        Call<Post> savePost(@Body User user);
    }
    

    然后你可以发送数据,

     User user = new User(); 
     user.setUsername("abcd"); 
     user.setPassword("password"); 
     public void sendPost(User user) { 
        mAPIService.savePost(user).enqueue(new Callback<Post>() { 
            @Override public void onResponse(Call<Post> call, Response<Post> response) { 
                if (response.isSuccessful()) { } 
                } 
            @Override public void onFailure(Call<Post> call, Throwable t) { }
    
        });
     } 
    

    您可以通过解析error 400

    Gson gson = new GsonBuilder().create(); 
    RetrofitError mError = gson.fromJson(response.errorBody().string(), RetrofitError.class); 
    Toast.makeText(context, mError.getMessages().getError().get(0).getMessage(),Toast.LENGTH_LONG).show(); 
    

    并添加 RetrofitError 类,

    public class RetrofitError {
        @SerializedName("messages")
        @Expose
        private Messages messages;
    
        public Messages getMessages() {
            return messages;
        }
    
        public void setMessages(Messages messages) {
            this.messages = messages;
        }
    }
    

    如果您有任何疑问,请联系my question。快乐的编码......随时问,如果有的话。 注意:添加 POJO

        public class Example {
    
    @SerializedName("UserLoginResult")
    @Expose
    private UserLoginResult userLoginResult;
    
    public UserLoginResult getUserLoginResult() {
    return userLoginResult;
    }
    
    public void setUserLoginResult(UserLoginResult userLoginResult) {
    this.userLoginResult = userLoginResult;
    }
    
    }
    

    --------用户登录结果 导入 com.google.gson.annotations.Expose; 导入 com.google.gson.annotations.SerializedName;

    public class UserLoginResult {
    
    @SerializedName("Email_id")
    @Expose
    private String emailId;
    @SerializedName("First_name")
    @Expose
    private String firstName;
    @SerializedName("Last_name")
    @Expose
    private String lastName;
    @SerializedName("Message")
    @Expose
    private String message;
    @SerializedName("Mobile_no")
    @Expose
    private String mobileNo;
    @SerializedName("Password")
    @Expose
    private String password;
    @SerializedName("RegID")
    @Expose
    private String regID;
    @SerializedName("Status")
    @Expose
    private String status;
    
    public String getEmailId() {
    return emailId;
    }
    
    public void setEmailId(String emailId) {
    this.emailId = emailId;
    }
    
    public String getFirstName() {
    return firstName;
    }
    
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    
    public String getLastName() {
    return lastName;
    }
    
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }
    
    public String getMessage() {
    return message;
    }
    
    public void setMessage(String message) {
    this.message = message;
    }
    
    public String getMobileNo() {
    return mobileNo;
    }
    
    public void setMobileNo(String mobileNo) {
    this.mobileNo = mobileNo;
    }
    
    public String getPassword() {
    return password;
    }
    
    public void setPassword(String password) {
    this.password = password;
    }
    
    public String getRegID() {
    return regID;
    }
    
    public void setRegID(String regID) {
    this.regID = regID;
    }
    
    public String getStatus() {
    return status;
    }
    
    public void setStatus(String status) {
    this.status = status;
    }
    
    }
    

    【讨论】:

    • 我已经在上面提到了您的链接,您可以查看..然后我现在又试了一次,不工作..我认为 json 解析还有其他问题
    • 响应{protocol=http/1.1, code=400, message=Bad Request, url=xxxx/UserLogin}
    • 是 Magento API 吗?你能解析错误信息吗
    • 在你的情况下结果是没有列表的 json 数据,而这里它在一个列表中,这就是区别,希望你明白我的意思。
    • 在列表前添加数组没问题,任何方式都可以解析错误
    【解决方案2】:

    感谢您的热心帮助@Subin Babu

    我对项目所做的更改

    1)添加了一个与参数同名的POJO类

    public class User {
    
    public String getMobile_no() {
        return Mobile_no;
    }
    
    public void setMobile_no(String mobile_no) {
        Mobile_no = mobile_no;
    }
    
    public String getPassword() {
        return Password;
    }
    
    public void setPassword(String password) {
        Password = password;
    }
    
    public String getRegID() {
        return RegID;
    }
    
    public void setRegID(String regID) {
        RegID = regID;
    }
    
    private String Mobile_no;
    private String Password;
    private String RegID;
    
    public User(String Mobile_no,String Password,String RegID)
    {
        this.Mobile_no = Mobile_no;
        this.Password = Password;
        this.RegID = RegID;
    }}
    

    2)将ApiInterface类更新为

    public interface ApiInterface {
    
    @POST("UserLogin")
    Call<SignUpResponse> registration(@Body User body);
    }
    

    3)将 MainActivity 类更新为

    User user = new User("xxxNumber", "rd", "555");
    
        Api.getClient().registration(user).enqueue(new Callback<SignUpResponse>() {
            @Override
            public void onResponse(Call<SignUpResponse> call, Response<SignUpResponse> response) {
                signUpResponsesData = response.body();
            }
    
            @Override
            public void onFailure(Call<SignUpResponse> call, Throwable t) {
    
            }
        });
    

    4)其他的都一样..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 2015-11-24
      • 2017-03-26
      • 2019-06-07
      • 2011-09-29
      • 2014-01-11
      • 2020-03-18
      相关资源
      最近更新 更多