Android Volley POST发送参数始终为null [英] Android Volley POST Sending Parameters is always null
本文介绍了Android Volley POST发送参数始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是android的新手,现在我正在做一个应用程序.为此,我需要将数据发送到服务器.现在我正在使用Volley post方法.但是当我使用volley将数据发送到服务器时,参数始终显示为null .在这里我附上了代码,请检查一下.在这里,我正在使用片段.
I am new in android.Now i am doing one application.For this one i need to send data into server.Now i am using Volley post method.But the parameters is always shows null when i send data into server using volley.here i attached the code please check it.Here i am using fragments.
代码部分
String url = "http://192.168.1.182:8084/name/registration.jsp";
final ProgressDialog pDialog = new ProgressDialog(this.getActivity());
pDialog.setMessage("Loading...");
pDialog.show();
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
JsonObjectRequest jsonObjReq = new JsonObjectRequest(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
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Ajay K K");
params.put("mailid", "ajaykk50@gmail.com");
params.put("phone", "8086327023");
params.put("place", "Calicut");
params.put("longitude","44444.3333");
params.put("latitude","666666.3333");
params.put("wheel", "1");
params.put("type", "owner");
return params;
}
};
// Adding request to request queue
rq.add(jsonObjReq);
推荐答案
请勿覆盖getParams()
. JsonObjectRequest
在构造函数中使用第三个参数来获取post参数.下面是凌空代码中包含的文档
Don't override getParams()
. JsonObjectRequest
uses third argument in constructor to get post parameters. below is documentation contained in volley's code
/**
* Creates a new request.
* @param method the HTTP method to use
* @param url URL to fetch the JSON from
* @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
* indicates no parameters will be posted along with request.
* @param listener Listener to receive the JSON response
* @param errorListener Error listener, or null to ignore errors.
*/
public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
}
这样的用法.
String url = "http://192.168.1.182:8084/name/registration.jsp";
final ProgressDialog pDialog = new ProgressDialog(this.getActivity());
pDialog.setMessage("Loading...");
pDialog.show();
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
JSONObject params = new JSONObject();
try {
params.put("name", "Ajay K K");
params.put("mailid", "ajaykk50@gmail.com");
params.put("phone", "8086327023");
params.put("place", "Calicut");
params.put("longitude","44444.3333");
params.put("latitude","666666.3333");
params.put("wheel", "1");
params.put("type", "owner");
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, params, //Not 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();
}
});
// Adding request to request queue
rq.add(jsonObjReq);
这篇关于Android Volley POST发送参数始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文