问题描述
这是我的android代码
JSONObject params = new JSONObject(); params.put("username","rajesh@gmail.com"); params.put("password","hellothere"); JsonObjectRequest loginRequest = new JsonObjectRequest( Request.Method.POST, "http://192.168.2.67/tmp/test.php", params, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Log.d("",""); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.d("",""); } }); requestQueue.add(loginRequest);
PHP中的服务器代码
<?php $usr = $_REQUEST['username']; $pwd = $_REQUEST['password']; $resp[$usr]=$pwd; echo json_encode($resp); ?>
我得到的回应是 {"":null}
我尝试了apache http letint,它普拉的工作 有没有办法,我可以用凌空做这个吗?
推荐答案
在请求身体中发送参数,您可以覆盖 getparams()或 getbody()方法的请求类
其他推荐答案
@Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("username", "SomeUser"); params.put("password", "blabla"); return params; }
这是您应该覆盖getparams()方法的方式.
问题描述
This is my android code
JSONObject params = new JSONObject(); params.put("username","rajesh@gmail.com"); params.put("password","hellothere"); JsonObjectRequest loginRequest = new JsonObjectRequest( Request.Method.POST, "http://192.168.2.67/tmp/test.php", params, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Log.d("",""); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.d("",""); } }); requestQueue.add(loginRequest);
The server code in php
<?php $usr = $_REQUEST['username']; $pwd = $_REQUEST['password']; $resp[$usr]=$pwd; echo json_encode($resp); ?>
the response i'm getting is {"":null}
I tried with apache http cleint and it worked prferectly is there any way i can do this with volley?
推荐答案
To send parameters in request body you need to override either getParams() or getBody() method of the request classes
Source: Asynchronous HTTP Requests in Android Using Volley
其他推荐答案
@Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("username", "SomeUser"); params.put("password", "blabla"); return params; }
This is the way you should override getParams() method.