我正在尝试在我的项目中使用volley来处理我的所有HTTP请求,因为据我所知,这是最高效的请求。因此,我通过遵循此AndroidHive教程开始学习排球。
我的第一个GET请求成功。然后我转到POST请求,但失败了。我在Stack Overflow上看到很多人在将volley的后期请求与PHP结合在一起时遇到问题。我相信我们无法使用常规方法来访问它,$_POST[""]因为凌空将JSON对象发送到我们指定的URL。
$_POST[""]
我尝试了很多解决方案,但没有成功。我猜应该有一种将volley与PHP结合使用的简单标准方法。所以我想知道我该怎么做才能在我的PHP代码中接收volley发送的json对象。
还有如何检查截击是否真的在发送JSON对象?
我的凌空代码发送简单的发帖请求:
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", "Droider"); return params; } }; // Adding request to request queue AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
我的用于接收json对象的PHP代码:( 我很确定这是错误的方式,我在PHP中不太好)
<?php $jsonReceiveData = json_encode($_POST); echo $jsonReceivedData; ?>
我也尝试了很多在PHP中接受JSON对象的方法 echo file_get_contents('php://input');
echo file_get_contents('php://input');
结果
null
编辑(感谢格鲁吉亚·贝纳托斯的正确方法)
正如您提到的,我创建了该类,其类名称CustomRequest如下:
CustomRequest
import java.io.UnsupportedEncodingException; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.HttpHeaderParser; public class CustomRequest extends Request<JSONObject>{ private Listener<JSONObject> listener; private Map<String, String> params; public CustomRequest(String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { super(Method.GET, url, errorListener); this.listener = reponseListener; this.params = params; } public CustomRequest(int method, String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { super(method, url, errorListener); this.listener = reponseListener; this.params = params; } @Override protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { return params; }; @Override protected void deliverResponse(JSONObject response) { listener.onResponse(response); } @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } }
现在在我的活动中,我打电话给以下人员:
String url = some valid url; Map<String, String> params = new HashMap<String, String>(); params.put("name", "Droider"); CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { Log.d("Response: ", response.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError response) { Log.d("Response: ", response.toString()); } }); AppController.getInstance().addToRequestQueue(jsObjRequest);
我的PHP代码如下:
<?php $name = $_POST["name"]; $j = array('name' =>$name); echo json_encode($j); ?>
现在,它返回正确的值:
Droider
我自己遇到了很多问题,请尝试一下!
public class CustomRequest extends Request<JSONObject> { private Listener<JSONObject> listener; private Map<String, String> params; public CustomRequest(String url,Map<String, String> params, Listener<JSONObject> responseListener, ErrorListener errorListener) { super(Method.GET, url, errorListener); this.listener = responseListener; this.params = params; } public CustomRequest(int method, String url,Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { super(method, url, errorListener); this.listener = reponseListener; this.params = params; } @Override protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { return params; }; @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } @Override protected void deliverResponse(JSONObject response) { listener.onResponse(response); }
的PHP
$username = $_POST["username"]; $password = $_POST["password"]; echo json_encode($response);
您必须制作一张地图,该地图支持键值类型,然后才能进行齐射。在php中,您将获得$ variable = $ _POST [“ key_from_map”]检索$ variable中的值,然后构建响应并对其进行json_encode。
这是一个如何查询sql并将答案以JSON回发的php示例
$response["devices"] = array(); while ($row = mysqli_fetch_array($result)) { $device["id"] = $row["id"]; $device["type"] = $row["type"]; array_push($response["devices"], $device); } $response["success"] = true; echo json_encode($response);
您可以在此处看到响应类型为JSONObject
public CustomRequest(int method, String url,Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener)
看一下听众的参数!