英文:
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
{
"id": "7fmVIBMLYiXRtTFOlxv90i"
}
and i want to make a PUT and DELETE requests to my android code
i made the requests like this
@PUT("tracks")
Call<likeDislike> putlike(@Body likeDislike like);
@HTTP(method = "DELETE", path = "tracks", hasBody = true)
Call<likeDislike> 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("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());
// }
// });
//
// }
`
答案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 = "DELETE", path = "remove/", hasBody = true)
Call<ResponseBody> deleteObject(@Body RequestBody object);
}
Reference : https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/HTTP.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论