Android截击Post请求。如何设置实体?
我在代码中使用了一个HttpPost,我在其中添加了如下实体:Android截击Post请求。如何设置实体?,android,http-post,content-type,android-volley,Android,Http Post,Content Type,Android Volley,我在代码中使用了一个HttpPost,我在其中添加了如下实体: String bodyContent = "......."; HttpPost httpost = new HttpPost(url); StringEntity se = new StringEntity(bodyContent); httpost.setEntity(se); 现在我想在截击请求中更改HttpPost。 如何设置setEntity? 我不明白如何插入字符串正文内容。 我应该用另一种方法吗 boolea
String bodyContent = ".......";
HttpPost httpost = new HttpPost(url);
StringEntity se = new StringEntity(bodyContent);
httpost.setEntity(se);
现在我想在截击请求中更改HttpPost。
如何设置setEntity?
我不明白如何插入字符串正文内容。
我应该用另一种方法吗
boolean contentType = true/false;
//new post request in to volley
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// response
Log.d("Error.Response", "OK");
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", "KO");
}
}
) {
//add body content-type
@Override
public String getBodyContentType() {
if (contentType) {
return "application/json; charset=utf-8";
} else {
return "text/plain; charset=utf-8";
}
}
//add header
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
Map<String, String> params = new HashMap<String, String>();
params.put("VLHASH", "464646");
return params;
}
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Alif");
params.put("domain", "http://itsalif.info");
return params;
}
};
queue.add(postRequest);
boolean contentType=true/false;
//截击中的新post请求
StringRequest postRequest=新的StringRequest(Request.Method.POST,url,
新的Response.Listener()
{
@凌驾
公共void onResponse(字符串响应){
//回应
Log.d(“错误响应”,“正常”);
}
},
新的Response.ErrorListener()
{
@凌驾
公共无效onErrorResponse(截击错误){
//错误
Log.d(“错误响应”,“KO”);
}
}
) {
//添加正文内容类型
@凌驾
公共字符串getBodyContentType(){
if(contentType){
返回“application/json;charset=utf-8”;
}否则{
返回“text/plain;charset=utf-8”;
}
}
//添加标题
@凌驾
公共映射getHeaders()引发AuthFailureError
{
Map params=新的HashMap();
参数put(“VLHASH”,“464646”);
返回参数;
}
@凌驾
受保护的映射getParams()
{
Map params=新的HashMap();
参数put(“名称”、“资格”);
参数put(“域”http://itsalif.info");
返回参数;
}
};
添加(postRequest);
您只需重写getBody(),就像您在StringRequest中重写了getBodyContentType一样
示例代码
你找到解决这个问题的办法了吗?
String body = "test body";
@Override
public byte[] getBody() throws AuthFailureError {
return body.getBytes();
}