Android-Volley无法将我的参数发送到服务器
我试图使用Android-Volley无法将我的参数发送到服务器,android,android-volley,Android,Android Volley,我试图使用Volley库将数据发送到服务器,但它给了我一个错误 “在的字符0处结束输入” 这是我的密码 public void postPrams(View view) { String tag_json_obj = "json_obj_req"; String url = "http://Urlhere.com/register.php"; final ProgressDialog pDialog = new ProgressDialog(t
Volley
库将数据发送到服务器,但它给了我一个错误
“在的字符0处结束输入”
这是我的密码
public void postPrams(View view) {
String tag_json_obj = "json_obj_req";
String url = "http://Urlhere.com/register.php";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
text.setText("done Post : "+response);
pDialog.hide();
Toast.makeText(getApplication(),"Done",Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("erorr", "Error: " + error.getMessage());
Toast.makeText(getApplication(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("first_name","Anwar");
params.put("last_name","Samir");
params.put("age", "1000");
params.put("country", "egypt");
params.put("city","le");
params.put("street", "10sq");
params.put("mobile_no", "0100000");
params.put("login_name", "Asi");
params.put("password", "123qwe");
return params;
}
};
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
public void postPrams(视图){
字符串tag_json_obj=“json_obj_req”;
字符串url=”http://Urlhere.com/register.php";
final ProgressDialog pDialog=新建ProgressDialog(本);
设置消息(“加载…”);
pDialog.show();
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Request.Method.POST,
url,空,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
text.setText(“完成后:+响应”);
pDialog.hide();
Toast.makeText(getApplication(),“Done”,Toast.LENGTH_LONG.show();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(“erorr”,“Error:+Error.getMessage());
Toast.makeText(getApplication(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
pDialog.hide();
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“名字”、“安瓦尔”);
参数put(“姓氏”、“萨米尔”);
参数put(“年龄”、“1000”);
参数put(“国家”、“埃及”);
参数put(“城市”、“乐”);
参数put(“街道”,“10平方”);
参数put(“手机号”、“0100000”);
参数put(“登录名”、“Asi”);
参数put(“密码”、“123qwe”);
返回参数;
}
};
AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);
}
请帮助我为什么会发生这种情况。请查看请求方法。发布或获取??你要退瓦卢吗?还是空白 在服务器端,尝试对参数进行回显 看一下凌空截击 将null替换为params。您无法在服务器端接收,因为您没有发送任何正文。您在其中放置参数的映射是针对标题的,而不是正文。您必须在中发送正文
JsonObjectRequest request = newJsonObjectRequest(method,url,body,listener){
....headers here
};
试试这个
public void postDataVolley(Context context,String url,JSONObject sendObj){
try {
RequestQueue queue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Volley", "Volley JSON post" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Volley", "Volley JSON post" + "That didn't work!");
}
});
queue.add(jsonObj);
}catch(Exception e){
}
}
public void postDataVolley(上下文、字符串url、JSONObject sendObj){
试一试{
RequestQueue=Volley.newRequestQueue(上下文);
JsonObjectRequest jsonObj=新的JsonObjectRequest(url,sendObj,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(“截击”,“截击后”+响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(“截击”,“截击JSON post”+“那不起作用!”);
}
});
add(jsonObj);
}捕获(例外e){
}
}
记录响应并在此处显示。查看源代码getParams()
用于表单数据getHeaders()
返回标题的映射getParams()
返回表单数据的映射。当我使用此代码时,请告诉我错误,并发送Toast消息,告诉我“org.json.JSONException:value谢谢所有这些代码解决了我的问题,你救了我一天
JSONObject params = new JSONObject();
params.put("first_name","Anwar");
params.put("last_name","Samir");
params.put("age", "1000");
params.put("country", "egypt");
params.put("city","le");
params.put("street", "10sq");
params.put("mobile_no", "0100000");
params.put("login_name", "Asi");
params.put("password", "123qwe");
JsonObjectRequest request = newJsonObjectRequest(method,url,body,listener){
....headers here
};
public void postDataVolley(Context context,String url,JSONObject sendObj){
try {
RequestQueue queue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Volley", "Volley JSON post" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Volley", "Volley JSON post" + "That didn't work!");
}
});
queue.add(jsonObj);
}catch(Exception e){
}
}