Android Volley似乎没有发布HTTP参数 [英] Android Volley does not seem to post HTTP parameters
问题描述
我已经在StackOverflow上看到了一些这样的问题,但是我似乎无法弄清楚为什么这段代码对我不起作用.
I've seen a few of these questions on StackOverflow but I can't seem to work out why this code is not working for me.
public void postMethod(final String msg) {
String url = "http://192.168.1.30/endpoint";
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Handle response...
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error...
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("msg", msg);
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;
}
};
queue.add(sr);
}
我正在尝试对运行python flask应用程序的服务器进行POST.如果我使用程序发出HTTP Post请求,则我的服务器应用程序可以正常运行.如果我使用AsyncTask并执行Volley试图为我做的事情,它也将正常工作.
I'm trying to make a POST to my server which is running a python flask application. If I use a program to make HTTP Post requests my server application works fine. It will also work fine if I use an AsyncTask and do what Volley is trying to do for me.
从所有示例中,我已经看到此代码应该可以正常工作,但是我的flask服务器从不接收任何POST参数.任何想法可能会发生什么?
From all of the examples I have seen this code should work fine but my flask server never receives any of the POST parameters. Any ideas what might be happening?
编辑
服务器代码:
from flask import Flask, request, abort
app = Flask(__name__)
@app.route('/endpoint', methods=['POST'])
def echo_msg():
if request.method == 'POST':
try:
msg = request.form['msg']
return msg
except KeyError, e:
# A required parameter is missing.
abort(400)
推荐答案
您需要重写getBodyContentType()并返回"application/x-www-form-urlencoded; charset = UTF-8";
You need to override getBodyContentType() and return "application/x-www-form-urlencoded; charset=UTF-8";
StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
getResources().getString(R.string.base_url),
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
MyFunctions.toastShort(LoginActivity.this, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("volley", "Error: " + error.getMessage());
error.printStackTrace();
MyFunctions.croutonAlert(LoginActivity.this,
MyFunctions.parseVolleyError(error));
loading.setVisibility(View.GONE);
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("username", etUname.getText().toString().trim());
params.put("password", etPass.getText().toString().trim());
return params;
}
};
AppController.getInstance().addToRequestQueue(jsonObjRequest);
这篇关于Android Volley似乎没有发布HTTP参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!