HTTP GET or HTTP POST or HTTP PUT, Android



这是我第一次尝试使用 Web API。我正在尝试为显示名称 https://github.com/Privo/PRIVO-Hub/wiki/Web-Services-API-Reference#update-user-display-name 实现此 PRIVO API。如您所见,没有太多的解释或代码示例。

我正在尝试做的是使用 HTTP GET 接收一个有效的响应,该响应表明名称格式正确,然后我应该将此显示名称保存在我们的 google 服务器上。我只需要上半场的帮助。稍后我会弄清楚如何保存到云服务器上。

我面临的问题是我不确定这是否是如何使用HTTP GET,而且我也不是100%确定我不应该使用POST或PUT。这就是我到目前为止所拥有的,请让我知道我做错了什么。

AsyncHttpClient client = null;
String authorizationHeader = "token_type", "" + " " + "access_token", "");
client.addHeader("Authorization", authorizationHeader);
client.addHeader("Content-type", "application/json");
client.addHeader("Accept", "application/json");
String requestBody = "displayName=" + "hardcodedDisplayName";
String requestURL = "https://privohub.privo.com/api/" + "account/public/saveDisplayName?" + requestBody;
client.get(requestURL, new ResponseHandler(myClass.this, "displayName", new OnResponseHandler() {
@Override
public void onSuccess(int statusCode, String apiName, JSONObject response) {
   if (statusCode == 200) {
      Log.i(TAG, "Seems to be working")
   }
}
@Override
public void onFailure(int statusCode, String apiName, String responseMessage) {
   Log.i(TAG, "Fail: " + responseMessage); 
}
@Override
public void onFailure(int statusCode, String apiName, JASONArray errorResponse) {
   Log.i(TAG, "Fail: " + errorResponse);
}
@Override
public void onFailure(int statusCode, String apiName, JSONObject errorResponse) {
   if (errorResponse != null) {
      Log.i(TAG, "Fail: " + errorResponse);
   }
}
}));   

由于上述代码,我得到了以下响应

{"message":"Object not found","validationErrors":[],"responseTimestamp":1419278367177,"totalCount":-1,"status":"failed","resultCount":-1,"entity":null}

此响应是什么意思?它在onFailure(int statusCode,String apiName,JSONObject errorResponse(上失败,错误代码为404

提前感谢!

您应该使用 API 文档所说的任何方法。在这种情况下,它既不是 GET 也不是 POST,而是 PUT!

最新更新