【问题标题】:LoopJ AndroidAsyncHttp returns code 415 when specifying RequestParamsLoopJ AndroidAsyncHttp 在指定 RequestParams 时返回代码 415
【发布时间】:2014-11-19 14:23:01
【问题描述】:

我正在使用 LoopJ AndroidAsyncHttp 从我的 android 应用程序发布异步 Http 调用。当我指定一些 RequestParams 时,调用一直失败,状态代码为 415(不支持的媒体类型)。一旦我删除 requestParams,呼叫就会通过,没有任何错误。

final AsyncHttpClient client;
String url = "http://someURL.com/SomeUserGUID/Profile/Statistics/Setup";
client = new AsyncHttpClient();

final RequestParams params = new RequestParams();
params.put("FirstClub", "false");
params.put("FairwayHit", "false");

Header[] headers = {
    new BasicHeader("Accept-Language", Locale.getDefault().toString())
    ,new BasicHeader("Authorization", ApplicationObject.companyBasicAuthString)
    ,new BasicHeader("Accept", "application/json")
};

client.setTimeout(60000);

client.post( ProfileActivity.this,url, headers, params, "application/json", new     AsyncHttpResponseHandler() {

@Override
public void onStart() {
    //Do something before start
    super.onStart();
}

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
    //some code
}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
    //some code
}
});

我的目标是让 URL 看起来像这样:String url = http://someURL.com/SomeUserGUID/Profile/Statistics/Setup?FirstClub=false& FairwayHit=false

我不希望将 URL 硬编码为一个字符串(顺便说一句,此方法有效),因为我有一些调用会创建一个非常长的 URL 字符串。

那么我怎样才能使用 AndroidAsyncHttp 实现成功的 post call 呢?

【问题讨论】:

    标签: android android-async-http


    【解决方案1】:

    415 不支持的媒体类型

    服务器拒绝为请求提供服务,因为请求的实体格式不受支持 由请求的方法请求的资源。

    您将 Content-Type 标头(在您的 post call 中)作为“application/json”发送,但您添加的参数不是 JSON。这可能就是发生错误的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-26
      • 2012-12-25
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多