本文介绍了凌空没有要求getParams()方法为标准的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想发布一些参数,我在Android中使用凌空轨API。这是code:
我试着用两个日志语句,一个在 getParams()方法
,另一个在 getHeaders()
。一个在 getHeaders()
的,而另一种是没有记录的。为什么凌空忽略 getParams()方法
?
{
// FULL_NAME,电子邮件,密码是私有变量定义为这个类
字符串URL =http://10.0.2.2:3000/users/sign_up.json;
JsonObjectRequest jsonObjReq =新JsonObjectRequest(Request.Method.POST,
URL,空,
新Response.Listener<的JSONObject>(){
@覆盖
公共无效onResponse(JSONObject的响应){
Log.d(TAG,response.toString());
pDialog.hide();
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
VolleyLog.d(TAG,错误:+ error.getMessage());
pDialog.hide();
}
}){
@覆盖
公共地图<字符串,字符串> getParams()方法{
地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
//这不会出现在日志中
Log.d(TAG,它分配PARAMS?);
params.put(姓名,full_name.getText()的toString());
params.put(电子邮件,email.getText()的toString());
params.put(密码,password.getText()的toString());
返回PARAMS;
}
@覆盖
公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
//这出现在日志中
Log.d(TAG,它分配头?);
HashMap的<字符串,字符串>标题=新的HashMap<字符串,字符串>();
headers.put(内容类型,应用/ JSON的;字符集= UTF-8);
返回头;
}
};
//添加请求,请求队列
。VHelper.getInstance()addToRequestQueue(jsonObjReq,tag_json_obj);
}
解决方案
在地方JsonObjectRequest的使用StringRequest
StringRequest SR =新StringRequest(Request.Method.POST,网址,新Response.Listener<字符串>(){
@覆盖
公共无效onResponse(字符串响应){
Log.d(TAG,response.toString());
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
VolleyLog.d(TAG,错误:+ error.getMessage());
Log.d(TAG,+ error.getMessage()+,+ error.toString());
}
}){
@覆盖
保护地图<字符串,字符串> getParams()方法{
地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
params.put(ID,28);
params.put(值,1);
返回PARAMS;
}
@覆盖
公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
地图<字符串,字符串>标题=新的HashMap<字符串,字符串>();
headers.put(内容类型,应用程序/ x-WWW的形式urlen codeD);
headers.put(ABC,价值);
返回头;
}
};
。AppController.getInstance()addToRequestQueue(SR);
I am trying to post some parameters to my rails API using Volley in Android. This is the code:
I tried with two log statements, one in getParams()
and another in getHeaders()
. The one in getHeaders()
is logged while the other one is not. Why is volley ignoring getParams()
?
{
//full_name,email,password are private variables defined for this class
String url = "http://10.0.2.2:3000/users/sign_up.json" ;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
}) {
@Override
public Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
//This does not appear in the log
Log.d(TAG,"Does it assign params?") ;
params.put("name", full_name.getText().toString());
params.put("email",email.getText().toString());
params.put("password", password.getText().toString());
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
//This appears in the log
Log.d(TAG,"Does it assign headers?") ;
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
// Adding request to request queue
VHelper.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
解决方案
Using StringRequest in place of JsonObjectRequest
StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, ""+error.getMessage()+","+error.toString());
}
}){
@Override
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("id", "28");
params.put("value", "1");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/x-www-form-urlencoded");
headers.put("abc", "value");
return headers;
}
};
AppController.getInstance().addToRequestQueue(sr);
这篇关于凌空没有要求getParams()方法为标准的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!