从API服务器获取400请求

时间:2019-09-09 11:04:55

标签: android json response bad-request

我正在使用POST请求的Android应用程序上工作 我收到400错误的请求错误

我认为可能是模型类错误,我不确定请回答

RetrofitInstance

public class RetrofitInstance {
private static Retrofit retrofit = null;
private static String BASE_URL = "http://<your ip address>:3000/";

public static Retrofit getService(){

    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    if(retrofit == null){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

    }
    return retrofit;
}
}    

API终点

public interface EndPoints {

    @FormUrlEncoded
    @POST("api/movie")
    Call<Movie> setMovie(@Field("title") String title,
                 @Field("genreId") String genreId,
                 @Field("numberInStock") int numberInStock,
                 @Field("dailyRentalRate") int dailyRentalRate);

    }

邮递员对API正在工作的请求和响应         https://ibb.co/TkvDZFz

模型类

流派Pojo:

public class Genre{

    @SerializedName("name")
    private String name;

    @SerializedName("_id")
    private String id;

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void setId(String id){
        this.id = id;
    }

    public String getId(){
        return id;
    }

    @Override
    public String toString(){
        return 
            "Genre{" + 
            "name = '" + name + '\'' + 
            ",_id = '" + id + '\'' + 
            "}";
        }
}

电影POJO:

public class Movie{

    @SerializedName("dailyRentalRate")
    private int dailyRentalRate;

    @SerializedName("__v")
    private int V;

    @SerializedName("genre")
    private Genre genre;

    @SerializedName("_id")
    private String id;

    @SerializedName("title")
    private String title;

    @SerializedName("numberInStock")
    private int numberInStock;

    @SerializedName("genreId")
    private String genreId;

    public Movie(String title, String genreId, int numberInStock ,int dailyRentalRate) {
        this.dailyRentalRate = dailyRentalRate;
        this.title = title;
        this.numberInStock = numberInStock;
        this.genreId = genreId;
    }

    public void setDailyRentalRate(int dailyRentalRate){
        this.dailyRentalRate = dailyRentalRate;
    }

    public int getDailyRentalRate(){
        return dailyRentalRate;
    }

    public void setV(int V){
        this.V = V;
    }

    public int getV(){
        return V;
    }

    public void setGenre(Genre genre){
        this.genre = genre;
    }

    public Genre getGenre(){
        return genre;
    }

    public void setId(String id){
        this.id = id;
    }

    public String getId(){
        return id;
    }

    public void setTitle(String title){
        this.title = title;
    }

    public String getTitle(){
        return title;
    }

    public void setNumberInStock(int numberInStock){
        this.numberInStock = numberInStock;
    }

    public int getNumberInStock(){
        return numberInStock;
    }

    @Override
    public String toString(){
        return 
            "Movie{" + 
            "dailyRentalRate = '" + dailyRentalRate + '\'' + 
            ",__v = '" + V + '\'' + 
            ",genre = '" + genre + '\'' + 
            ",_id = '" + id + '\'' + 
            ",title = '" + title + '\'' + 
            ",numberInStock = '" + numberInStock + '\'' + 
            "}";
        }
}

活动

public class MainActivity extends AppCompatActivity {

private Button click;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    click = findViewById(R.id.click);

    click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addMovieCall();
        }
    });
}

private void addMovieCall() {
    final EndPoints vidlyEndPoint = RetrofitInstance.getService().create(EndPoints.class);
    Call<Movie> call =  vidlyEndPoint.setMovie("intelRPG","5d7398d2bdc4d04960d60845",20,30);

    call.enqueue(new Callback<Movie>() {
        @Override
        public void onResponse(Call<Movie> call, Response<Movie> response) {
            if(!response.isSuccessful()){
                Log.e("Response", String.valueOf(response.code()));
                Log.e("Response", String.valueOf(response.message()));
            }
            else if(response.isSuccessful()){
                Log.e("Response", String.valueOf(response.body().getId()));
                Log.e("Response", String.valueOf(response.body().getTitle()));
                Log.e("Response", String.valueOf(response.body().getGenre().getName()));
                Log.e("Response", String.valueOf(response.body().getDailyRentalRate()));
                Log.e("Response", String.valueOf(response.body().getNumberInStock()));
                Toast.makeText(getApplicationContext(),"Posted Successfully",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<Movie> call, Throwable t) {
            Log.e("Response failed",t.getMessage());
        }
    });
}
 }

我希望输出为200,但实际输出为400。

3 个答案:

答案 0 :(得分:2)

您必须将值设置为与所需类型相同的类型

@FormUrlEncoded
@POST("api/movie")
Call<Movie> setMovie(@Field("title") String title,
             @Field("genreId") String genreId,
             @Field("numberInStock") String numberInStock, 
             @Field("dailyRentalRate") String dailyRentalRate);

}

我还建议使用带有@Body批注的One pojo类。

顺便说一句,为了更好地理解网络问题,您最好使用 chuck 库,只需在okhttp对象中添加Chuck作为拦截器即可。它将显示出真正的原因,帮助您更快地找到问题

https://github.com/jgilfelt/chuck

答案 1 :(得分:1)

更改界面呼叫:

Call<Movie> setMovie(@Field("title") String title,
                     @Field("genreId") String genreId,
                     @Field("numberInStock") String numberInStock, // Needs String
                     @Field("dailyRentalRate") String dailyRentalRate);

    }

,呼叫将为:

Call<Movie> call =  vidlyEndPoint.setMovie("intelRPG","5d7398d2bdc4d04960d60845","20","30");

答案 2 :(得分:0)

您可以尝试以下操作。

JsonObject requestBody = new JsonObject();
            requestBody.addProperty("title", "intelRPG");
            requestBody.addProperty("genreId", "5d7398d2bdc4d04960d60845");
            requestBody.addProperty("numberInStock", "20");
            requestBody.addProperty("dailyRentalRate", "30");

Call<Movie> call =  vidlyEndPoint.setMovie(requestBody);
.....

API结束点应如下所示

@POST("api/movie")
Call<Movie> setMovie(@Body JsonObject jsonObject);

希望它对您有帮助,快乐编码