【问题标题】:LoopJ AndroidAsyncHttp PUT, how to do?LoopJ AndroidAsyncHttp PUT,怎么办?
【发布时间】:2014-12-23 19:40:03
【问题描述】:

我需要一些帮助来纠正我使用以下 API 的方法 > https://github.com/Privo/PRIVO-Hub/wiki/Web-Services-API-Reference#update-user-display-name

我不确定如何使用 LoopJ AndroidAsyncHttp PUT 发送 JSON 对象进行更新。我收到以下错误响应

{"message":"Error: null","validationErrors":[]."responseTimestamp":141936124612,"totalCount":-1,"status":"failed","re​​sultCount":-1,"实体":null}

我怎么做错了?

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

})); 

【问题讨论】:

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


【解决方案1】:

看起来你正在使用AsyncHttpClient

使用 preparePutAsyncHttpClient.BoundRequestBuilder 构建器,就像在 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.
        }
    });

【讨论】:

  • asyncHttpClient.preparePut 应该是 client.preparePut?我在 asyncHttpClient.preparePut 上收到错误。
  • 是的,你是对的。检查编辑。正在使用这个 AsyncHttpClient 库 github.com/AsyncHttpClient/async-http-client 吗?
  • 我没有使用那个库。我的 AsyncHttpClient 有包 com.loopj.android.http.AsyncHttpClient。对于 AsyncHttpClient 的客户端对象,preparePut 未定义。我会 +1 帮助,这是我最接近的。谢谢!
  • 嘿,这就解释了。
猜你喜欢
  • 1970-01-01
  • 2015-01-26
  • 2012-12-25
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
相关资源
最近更新 更多