【发布时间】:2016-05-15 21:12:21
【问题描述】:
我正在尝试使用 Android 应用程序中的 OkHttp 在我的服务器上调用 put 方法。
这是 api 方法签名:
public void Put(int userId, string regId)
{
}
这是调用上述方法的Android代码:
private boolean SendGCMRegIdToServer(String registrationId, Integer userId) throws IOException {
HttpUrl url = new HttpUrl.Builder()
.scheme("http")
.host(serverApiHost)
.addPathSegment("AppDashboard")
.addPathSegment("api")
.addPathSegment("GCM/")
.build();
MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
String json = "{'userId':" + userId + ","
+ "'regId':'" + registrationId + "'"
+ "}";
RequestBody requestBody = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.put(requestBody)
.build();
//this should post the data to my server
Response response = client.newCall(request).execute();
if(response.code() == 400)
return false;
return true;
}
现在的问题是我在响应中收到错误代码 405,说方法不允许,但我看不出问题出在哪里,因为我可以在服务器本身上使用 Postman 成功调用该方法,如下所示:
http://localhost/AppDashboard/api/GCM?userId=5®Id=123
我认为这可能与在 JSON 字符串中错误传递的整数或字符串有关,但不明白为什么这不起作用。
【问题讨论】:
-
你确定你也在邮递员中使用了 PUT 吗?
标签: android asp.net-web-api okhttp http-status-code-405