如何在带有参数的改造2中调用DELETE方法

时间:2019-09-17 05:38:35

标签: android api retrofit2

我正在使用retrofit2调用delete api,它在邮递员中正常工作,但在应用程序中出现以下错误

Response{protocol=http/1.0, code=405, message=METHOD NOT ALLOWED, url=http://192.168...

这是我的基本网址

public static final String BASE_URL = "http://192.168.1.127:3222/";

@DELETE("student/{firstname}/{lastname}")
Call<ResponseBody> deleteStudent(@Path("firstname") String firstname, @Path("lastname") String lastname);

和我这样称呼的Java文件

Call<ResponseBody> call = interfaces.deleteStudent(FirstName,LastName);```

Postman screenshot here

5 个答案:

答案 0 :(得分:1)

@FormUrlEncoded
@HTTP(method = "DELETE", path = "student/{firstname}/{lastname}")
Call<ResponseBody> deleteStudent(@Field("firstname") String firstname, @Field("lastname") String lastname);

如果上述方法无效,请尝试

@FormUrlEncoded
@HTTP(method = "DELETE", path = "student/{firstname}/{lastname}",hasBody = true)
Call<ResponseBody> deleteStudent(@Field("firstname") String firstname, @Field("lastname") String lastname);

或者您也可以参考此答案 How to send a HTTP-delete with a body in retrofit?

答案 1 :(得分:1)

已更新,感谢您的推荐@GovindPrajapati 我只需做些小改动即可添加 @Field ,它对我有用。

@FormUrlEncoded
    @HTTP(method = "DELETE", path = "student",hasBody = true)
    Call<ResponseBody> deleteStudent(@Field("firstname") String firstname, @Field("lastname") String lastname);

答案 2 :(得分:0)

您可能会错过基本网址中的“ /”

字符串baseURL =“ http // www.example.com /”

http // www.example.com-这不适用于您的情况

答案 3 :(得分:0)

我认为您应该将服务网址设置为@DELETE(“ here”),例如“ http.example.com/api/login”。

答案 4 :(得分:0)

由于邮递员@FormUrlEncoded方法不允许,因为数据是以form-data的形式发送到api的,所以请使用

@Multipart
@DELETE("/student")
Call<MemberListResponse> deleteStudent(@Part ("firstname") RequestBody first_name,
                                   @Part ("lastname") RequestBody last_name);

这里是如何从String获取requestBody。

 String somevalue = "somevalue";
    RequestBody FirstName= RequestBody.create(MediaType.parse("text/plain"), somevalue);

String somevalue1 = "somevalue";
    RequestBody LastName= RequestBody.create(MediaType.parse("text/plain"), somevalue1);

Call<ResponseBody> call = interfaces.deleteStudent(FirstName,LastName);