2014-12-22 99 views
0

這是我第一次嘗試使用Web API。我正試圖實現這個PRIVO API顯示名稱https://github.com/Privo/PRIVO-Hub/wiki/Web-Services-API-Reference#update-user-display-name。正如你所看到的,沒有太多的解釋或代碼示例。HTTP GET或HTTP POST或HTTP PUT,Android

我想要做的是使用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); 
    } 
} 

})); 

我得到在上面的代碼的結果如下響應

{ 「消息」: 「未找到對象」, 「validationErrors」:[], 「responseTimestamp」:1419278367177, 「TOTALCOUNT」 :-1,「status」:「failed」,「resultCount」: - 1,「entity」:null}

這個迴應是什麼意思?它失敗onFailure(int statusCode,字符串apiName,JSONObject errorResponse)和錯誤代碼是404

在此先感謝!

回答

1

您應該使用API​​文檔中說明您應該使用的任何方法。在這種情況下,它不是GET或POST,而是PUT!

+0

我現在看到這個錯誤。但是,你能提供一個例子嗎?方法與GET類似嗎?我可以簡單地改變放置是我的意思嗎? – portfoliobuilder

+0

是的,client.put()應該可以工作。可能需要將參數編碼爲RequestParam對象,而不是追加到url。 – wvdz

+0

這是我第一次這樣做。你可以通過將參數編碼爲RequestParam對象,向我展示你的意思嗎? – portfoliobuilder