Search code examples
androidhttpasp.net-web-apiandroid-async-http

LoopJ AndroidAsyncHttp PUT, how to do?


I need some help correcting my approach for using the following API > https://github.com/Privo/PRIVO-Hub/wiki/Web-Services-API-Reference#update-user-display-name.

I am unsure of how to send a JSON object for update using LoopJ AndroidAsyncHttp PUT. I am receiving the following error response

{"message":"Error: null","validationErrors":[]."responseTimestamp":141936124612,"totalCount":-1,"status":"failed","resultCount":-1,"entity":null}

How am I doing this wrong?

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.put(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);
   }
}

})); 

Solution

  • Looks like you are using AsyncHttpClient,

    Use the preparePut and AsyncHttpClient.BoundRequestBuilder builder as in the examples/readme,

    AsyncHttpClient client =  new AsyncHttpClient(); // not null
    // other code here
    String requestBody = "displayName=" + "hardcodedDisplayName";
    String requestURL = "https://privohub.privo.com/api/" + "account/public/saveDisplayName?" + requestBody;
    client.preparePut(requestURL)  // sets the urls the put request and gets a AsyncHttpClient.BoundRequestBuilder
       .setBody(requestBody) // sets the body of the put request
       .execute(new AsyncCompletionHandler<Response>(){
    
            @Override
            public Response onCompleted(Response response) throws Exception{
                // Do something with the Response
                // ...
                return response;
            }
    
            @Override
            public void onThrowable(Throwable t){
                // Something wrong happened.
            }
        });