如何使用setEntity在Android的排球? [英] How to use setEntity in Android Volley?

查看:118
本文介绍了如何使用setEntity在Android的排球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在httpPost我们setEntity(新StringEntity)。但我使用抽射现在。我想使用setEntity方法凌空。我该怎么做?

我想用Twitter的API类似这样使用它;

  HttpPost httpPost =新HttpPost(TwitterTokenURL);
httpPost.setHeader(授权,基本+ base64En codeD);
httpPost.setHeader(内容类型,应用程序/ X WWW的形式 - urlen codeD;字符集= UTF-8);
httpPost.setEntity(新StringEntity(grant_type = client_credentials));


解决方案

@覆盖 getBodyContentType() getBody()在延长请求< T> 使用类似于下面的内容类:

  @覆盖
公共字符串getBodyContentType(){
    返回entity.getContentType()的getValue()。
}@覆盖
公众的byte [] getBody()抛出AuthFailureError {
    ByteArrayOutputStream的OutputStream =新ByteArrayOutputStream();
    尝试{
        entity.writeTo(OutputStream的);
    }赶上(IOException异常五){
        VolleyLog.e(IOException异常@+的getClass()getSimpleName());
    }
    返回outputStream.toByteArray();
}

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

解决方案

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

这篇关于如何使用setEntity在Android的排球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆