0

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);
   }
}

})); 
Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29
portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
  • curious about the choice for a client in android... http://android-developers.blogspot.com/2011/09/androids-http-clients.html .. where did you come up with java httpclient on android? i would think ( loopj, volley, okhttp, apache's android client ) may be better fits?? – Robert Rowntree Dec 23 '14 at 20:42
  • I am trying to use loopj. http://grepcode.com/file/repo1.maven.org/maven2/com.loopj.android/android-async-http/1.4.3/com/loopj/android/http/AsyncHttpClient.java. Thing is, this is my first attempt at it. I think my params for PUT may be incorrect maybe. – portfoliobuilder Dec 23 '14 at 20:44

1 Answers1

1

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.
        }
    });
petey
  • 16,914
  • 6
  • 65
  • 97
  • The asyncHttpClient.preparePut should be client.preparePut? I am receiving errors on asyncHttpClient.preparePut. – portfoliobuilder Dec 23 '14 at 20:15
  • yep you are right. Check the edit. Are using this AsyncHttpClient library https://github.com/AsyncHttpClient/async-http-client ? – petey Dec 23 '14 at 20:18
  • I am not using that library. My AsyncHttpClient has the package com.loopj.android.http.AsyncHttpClient. preparePut is undefined for my client object for AsyncHttpClient. I will +1 for helping, this is closest I have gotten with this. Thanks! – portfoliobuilder Dec 23 '14 at 20:22
  • heh, well that explains that. – petey Dec 23 '14 at 20:27