【问题标题】:How to use setEntity in Android Volley?如何在 Android Volley 中使用 setEntity?
【发布时间】:2014-04-07 18:15:38
【问题描述】:

在 httpPost 我们设置实体(新字符串实体)。但我现在正在使用凌空抽射。我想在 volley 中使用该 setEntity 方法。我该怎么做?

我想像这样将它与 Twitter api 一起使用;

HttpPost httpPost = new HttpPost(TwitterTokenURL);
httpPost.setHeader("Authorization", "Basic " + base64Encoded);
httpPost.setHeader("Content-Type", "application/x-www-form-  urlencoded;charset=UTF-8");
httpPost.setEntity(new StringEntity("grant_type=client_credentials"));

【问题讨论】:

  • 可能在 set headers 方法中,你必须重写 Request 对象才能在 volley 中做自定义的事情
  • 嗨,我也在尝试做同样的事情,需要设置 StringEntity。你能帮我解决这个问题吗?你是怎么解决的。

标签: android apache android-volley


【解决方案1】:

@Override getBodyContentType()getBody() 在您的扩展 Request<T> 类中使用类似于以下内容:

@Override
public String getBodyContentType() {
    return entity.getContentType().getValue();
}

@Override
public byte[] getBody() throws AuthFailureError {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        entity.writeTo(outputStream);
    } catch (IOException e) {
        VolleyLog.e("IOException @ " + getClass().getSimpleName());
    }
    return outputStream.toByteArray();
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 2013-07-08
  • 2021-12-11
  • 2017-03-19
相关资源
最近更新 更多