如何在Android Volley中使用setEntity?

时间:2014-04-07 18:15:39

标签: android apache android-volley

在httpPost中我们设置了setEntity(new StringEntity)。但我现在正在使用截击。我想在排球中使用该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"));

1 个答案:

答案 0 :(得分:4)

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