【问题标题】:How to call DELETE method in retrofit 2 with params如何在改造 2 中使用参数调用 DELETE 方法
【发布时间】:2020-01-17 22:40:36
【问题描述】:

我正在使用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);```

【问题讨论】:

  • 请发布完整的错误堆栈?还显示您的基本网址
  • stackoverflow.com/questions/6272381/… 答案已经在这里给出,请检查。 "405 方法不允许在 android 中出现错误请这个关键字得到你的答案"
  • 不,这与我的错误无关,我在删除 api 中收到此错误,只有在我的网址对所有 @YogeshBorhade 保持相同的情况下

标签: android api retrofit2


【解决方案1】:

更新感谢您的参考@GovindPrajapati 我必须做些小改动添加 @Field,它对我有用。

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

【讨论】:

    【解决方案2】:

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

    String baseURL="http//www.example.com/"

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

    【讨论】:

    • 不,我的基本网址非常适合其他获取和发布 api "192.168.1.127:3222"
    • @yuvrajsinh 我没有告诉你的 baseURL 有问题。您错过了 baseURL 中的反斜杠。请再读一遍我的回答
    • 请检查我已经添加了与 / @RanjithKumar 相关的基本网址
    【解决方案3】:

    我认为您应该将服务 url @DELETE("here") 设置为“http.example.com/api/login”。

    【讨论】:

      【解决方案4】:
      @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?

      【讨论】:

      • 它在路径中得到错误是有帮助的,所以我将它更改为字段及其工作
      【解决方案5】:

      根据邮递员@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);
      

      【讨论】:

        【解决方案6】:

        你可以试试这个,它对我有用: public static final String BASE_URL = "http://192.168.1.127:3222/";

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

        【讨论】:

          猜你喜欢
          • 2020-01-31
          • 2021-09-25
          • 1970-01-01
          • 2016-08-15
          • 2021-09-27
          • 2011-12-23
          • 2014-02-17
          • 1970-01-01
          • 2021-05-24
          相关资源
          最近更新 更多