PUT和DELETE在Retrofit中的使用

huangapple go评论87阅读模式
英文:

PUT and DELETE in RETROFIT

问题

private void putlike() {

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("jasonfakeAPI/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);

    likeDislike likee = new likeDislike("ubvjksdbvxnudb");
    Call<likeDislike> likeee = jsonPlaceHolderApi.putlike(likee);

    likeee.enqueue(new Callback<likeDislike>() {
        @Override
        public void onResponse(Call<likeDislike> call, Response<likeDislike> response) {
            if (!response.isSuccessful()) {
                textViewResult.setText("Code: " + response.code());
                return;
            }

            likeDislike likeResponse = response.body();
            String content = "";
            content += "Code:" + response.code() + "\n";
            content += "ID: " + likeResponse.getId() + "\n";
            textViewResult.setText(content);
        }

        @Override
        public void onFailure(Call<likeDislike> call, Throwable t) {
            textViewResult.setText(t.getMessage());
        }
    });
}

// private void deletelike() {
//     Retrofit retrofit = new Retrofit.Builder()
//             .baseUrl("jasonfakeAPI/")
//             .addConverterFactory(GsonConverterFactory.create())
//             .build();
//     JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
//
//     Call<Void> likeee = jsonPlaceHolderApi.deletelike("7fmVIBMLYiXRtTFOlxv90i");
//
//     likeee.enqueue(new Callback<Void>() {
//         @Override
//         public void onResponse(Call<Void> call, Response<Void> response) {
//             textViewResult.setText("Code: " + response.code());
//         }
//
//         @Override
//         public void onFailure(Call<Void> call, Throwable t) {
//             textViewResult.setText(t.getMessage());
//         }
//     });
// }
英文:

Hi everyone i do have aserver with data that looks like this
someurl/jasonfakeAPI/tracks

    {
&quot;id&quot;: &quot;7fmVIBMLYiXRtTFOlxv90i&quot;
}

and i want to make a PUT and DELETE requests to my android code
i made the requests like this

    @PUT(&quot;tracks&quot;)
Call&lt;likeDislike&gt; putlike(@Body likeDislike like);
@HTTP(method = &quot;DELETE&quot;, path = &quot;tracks&quot;, hasBody = true)
Call&lt;likeDislike&gt; deletelike(@Body likeDislike like);

and in my code i implemented the functions like this but the delete i got a 404 i need help with this issue and are my methods correct ?

`
private void putlike() {

        Retrofit retrofit = new Retrofit.Builder()
.baseUrl(&quot;jasonfakeAPI/&quot;)
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
likeDislike likee=new likeDislike(&quot;ubvjksdbvxnudb&quot;);
Call&lt;likeDislike&gt; likeee = jsonPlaceHolderApi.putlike(likee);
likeee.enqueue(new Callback&lt;likeDislike&gt;() {
@Override
public void onResponse(Call&lt;likeDislike&gt; call, Response&lt;likeDislike&gt; response) {
if (!response.isSuccessful()) {
textViewResult.setText(&quot;Code: &quot; + response.code());
return;
}
likeDislike likeResponse = response.body();
String content = &quot;&quot;;
content += &quot;Code:&quot; + response.code()+&quot;\n&quot;;
content += &quot;ID: &quot; + likeResponse.getId() + &quot;\n&quot;;
textViewResult.setText(content);
}
@Override
public void onFailure(Call&lt;likeDislike&gt; call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
//    private void deletelike() {
//
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(&quot;jasonfakeAPI/&quot;)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
//
//
//        Call&lt;Void&gt; likeee = jsonPlaceHolderApi.deletelike(&quot;7fmVIBMLYiXRtTFOlxv90i&quot;);
//
//        likeee.enqueue(new Callback&lt;Void&gt;() {
//            @Override
//            public void onResponse(Call&lt;Void&gt; call, Response&lt;Void&gt; response) {
//                textViewResult.setText(&quot;Code: &quot; + response.code());
//            }
//
//            @Override
//            public void onFailure(Call&lt;Void&gt; call, Throwable t) {
//                textViewResult.setText(t.getMessage());
//            }
//        });
//
//    }

`

答案1

得分: 2

这个注解还可以用于发送带有请求体的 DELETE 请求:

interface Service {
   @HTTP(method = "DELETE", path = "remove/", hasBody = true)
   Call<ResponseBody> deleteObject(@Body RequestBody object);
}

参考链接: https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/HTTP.html

英文:

This annotation can also used for sending DELETE with a request body:

interface Service {
@HTTP(method = &quot;DELETE&quot;, path = &quot;remove/&quot;, hasBody = true)
Call&lt;ResponseBody&gt; deleteObject(@Body RequestBody object);
}

Reference : https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/HTTP.html

huangapple
  • 本文由 发表于 2020年4月6日 09:03:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61051532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定