Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setEntity in Android Volley?

In httpPost we setEntity(new StringEntity). But I'm using volley right now. I'd like to use that setEntity method in volley. How can I do that?

I would like to use it with Twitter api like this;

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"));
like image 645
Can Uludağ Avatar asked Apr 07 '14 18:04

Can Uludağ


1 Answers

@Override getBodyContentType() and getBody() in your extended Request<T> class using something similar to the following:

@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();
}
like image 131
Submersed Avatar answered Sep 19 '22 22:09

Submersed