【问题标题】:HTTP GET or HTTP POST or HTTP PUT, AndroidHTTP GET 或 HTTP POST 或 HTTP PUT,Android
【发布时间】:2014-12-23 05:04:58
【问题描述】:

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

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

我面临的问题是我不确定这是否是使用 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":"找不到对象","validationErrors":[],"responseTimestamp":1419278367177,"totalCount":-1,"status":"failed","re​​sultCount":-1,"实体":null}

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

提前致谢!

【问题讨论】:

    标签: android http asp.net-web-api


    【解决方案1】:

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

    【讨论】:

    • 我现在看到了这个错误。但是,你能举个例子吗?该方法是否类似于 GET?我可以简单地改变 get to put 就是我的意思吗?
    • 是的,client.put() 应该可以工作。也许需要将参数编码为 RequestParam 对象,而不是附加到 url。
    • 这是我第一次这样做。您能否通过将参数编码为 RequestParam 对象来举例说明您的意思?
    • 我假设您使用的是 loopj?我也没有使用该库的经验,但我从 javadoc 页面假设您应该使用 onbe 的 put 方法:loopj.com/android-async-http/doc/com/loopj/android/http/… RequestParam 的 javadoc 包含如何添加参数的示例:loopj.com/android-async-http/doc/com/loopj/android/http/…跨度>
    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2011-12-04
    • 2015-08-08
    相关资源
    最近更新 更多