Android 截击不通过post参数
我在Android项目中使用Volley将其连接到服务器 我需要使用Android 截击不通过post参数,android,http-post,android-volley,Android,Http Post,Android Volley,我在Android项目中使用Volley将其连接到服务器 我需要使用post方法将数据发送到url并获得响应,但截击无法传递我的数据 这是我的密码: StringRequest jsonObjReq = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() { @Override public void
post
方法将数据发送到url并获得响应,但截击无法传递我的数据
这是我的密码:
StringRequest jsonObjReq = new StringRequest(Request.Method.POST,url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jres=new JSONObject();
jres.opt(response);
if(jres.getString("value").toLowerCase()=="true"){
ConfirmMob=txtMob.getText().toString();
SubmitMob();
}
else{
AlertDialog.Builder dialog=new AlertDialog.Builder(App.getContext());
dialog.setMessage(R.string.IncorrectMob);
dialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
loading.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("IsMobValid - " + error.networkResponse, "Error: " + error.getMessage());
error.printStackTrace();
error.networkResponse
loading.hide();
}
}){
@Override
protected Map<String,String> getParams()throws AuthFailureError{
Map<String,String> params = new HashMap<String, String>();
params.put("x", "IsMobValid");
params.put("Mob", txtMob.getText().toString());
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
App.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
StringRequest-jsonObjReq=new-StringRequest(Request.Method.POST,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jres=新的JSONObject();
jres.opt(响应);
if(jres.getString(“value”).toLowerCase()=“true”){
ConfirmMob=txtMob.getText().toString();
SubmitMob();
}
否则{
AlertDialog.Builder dialog=新建AlertDialog.Builder(App.getContext());
dialog.setMessage(R.string.IncorrectMob);
dialog.show();
}
}捕获(JSONException e){
e、 printStackTrace();
}
loading.hide();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(“IsMobValid-”+error.networkResponse,“error:”+error.getMessage());
错误。printStackTrace();
error.networkResponse
loading.hide();
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“x”,“IsMobValid”);
参数put(“Mob”,txtMob.getText().toString());
返回参数;
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/x-www-form-urlencoded”);
返回参数;
}
};
App.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);
我怎样才能把它修好?
有什么不对吗?我认为凌空抽射有缺陷 通过
JsonObjectRequest
进行请求时,从未传递任何参数。
在通过StringRequest
请求时,如果发送自定义headaer,听起来HTTPRequest是不正常的。因为我的服务器导致500错误
通过删除下面的代码,我的问题就解决了
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
@覆盖
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/x-www-form-urlencoded”);
返回参数;
}
JSON请求都失败了,因为在服务器端,所有POST参数都是空的!我的带有Express的node.js服务器使用的是Express.bodyparser(),它以前在本地运行得很好。
还有一点很奇怪,那就是标题工作得很好
经过大量的搜索,并在截击用户组中寻求帮助,我发现了一个技巧,在内容类型标题上设置字符集。因为没有它,bodyparser无法正确解析该参数。
因此,正确的内容类型标题应为:
应用程序/json;字符集=utf-8
并且在第25行设置了正确的内容类型标题:
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("param1", youParameter);
JsonObjectRequest myRequest = new JsonObjectRequest(
Request.Method.POST,
"https://your.url/here",
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
verificationSuccess(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
verificationFailed(error);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", "My useragent");
return headers;
}
};
MyApplication.getInstance().addToRequestQueue(myRequest, "tag");
Map jsonParams=newhashmap();
jsonParams.put(“param1”,youParameter);
JsonObjectRequest myRequest=新的JsonObjectRequest(
Request.Method.POST,
"https://your.url/here",
新的JSONObject(jsonParams),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
验证成功(响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
验证失败(错误);
}
}) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
headers.put(“用户代理”、“我的用户代理”);
返回标题;
}
};
MyApplication.getInstance().addToRequestQueue(myRequest,“标记”);
您是否收到任何错误消息OneErrorResponse
?否@yaa110。在服务器端发生错误,因为请求需要一些参数,但在我的请求截击中不将它们发送到服务器您启动了请求队列吗?在上述代码的最后一行中,当请求添加到请求队列中时,请求启动@ElDuderino