[英]Retrofit - POST request with @Path and without parameters
The request to API as follows: 对API的请求如下:
POST /verify/phone/{userid}/
POST / verify / phone / {userid} /
Mandatory parameters: userid (integer)
必填参数:userid(整数)
Return: nothing
返回:无
I receive Bad Request
error all the time if I want to verify user with id 1 (for example): 如果我想验证ID为1的用户(例如),总是会收到
Bad Request
错误:
Response{protocol=http/1.1, code=400, message=Bad Request, url=http://dummyapi.com/verify/phone/1/}
Posting the same request using Postman works well. 使用Postman发布相同的请求效果很好。
Code : 代码 :
@POST("/verify/phone/{userid}/") Call<String> postVerifyPhone(@Path("userid") int userId);
public void postVerifyPhone(int userID){
showProgressBar();
RestClient.GitApiInterface service = RestClient.getClient();
Call<String> call = service.postVerifyPhone(userID);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response) {
hideProgressBar();
if (response.isSuccess()) {
String result = response.body();
} else {
Log.d("Fail: ", response.body()); //everytime here with Bad Request
}
}
@Override
public void onFailure(Throwable t) {
hideProgressBar();
}
});
}
I'm using String as the return type since there's message returned when there're no user with requested ID in db. 我使用String作为返回类型,因为当db中没有用户具有请求的ID时会返回消息。
What could be a cause? 可能是什么原因?
I am calling below code for reading response data. 我在下面的代码中调用以读取响应数据。
public void postVerifyPhone(int userID){
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Constants.URL).build();
RetrofitApi retrofitApi = restAdapter.create(RetrofitApi.class);
retrofitApi.postVerifyPhone(userID, new Callback<Response>() {
@Override
public void success(Response response, Response response2) {
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(response.getBody().in()));
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
String result = sb.toString();
// System.out.println("res:" + result.toString());
}
@Override
public void failure(RetrofitError error) {
hidePDialog();
}
});
}
below is postVerifyPhone() code 下面是postVerifyPhone()代码
@FormUrlEncoded
@POST("/verify/phone/")
public void postVerifyPhone(@Field("userID") String userID, Callback<Response> resonse);
It is working fine for me.please check it once. 对我来说很好,请检查一次。
我认为正确的注释是@PathParam,而不是@Path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.