栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Volley发送帖子请求并使用PHP接收

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Volley发送帖子请求并使用PHP接收

我自己遇到了很多问题,请尝试一下!

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;}@Overrideprotected Map<String, String> getParams() throws com.android.volley.AuthFailureError {    return params;};@Overrideprotected 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));    }}@Overrideprotected void deliverResponse(JSonObject response) {    listener.onResponse(response);}

的PHP

$username = $_POST["username"];$password = $_POST["password"];echo json_enpre($response);

您必须制作一张地图,该地图支持键值类型,然后才能进行齐射。在php中,您将获得$ variable = $ _POST [“
key_from_map”]检索$ variable中的值,然后构建响应并对其进行json_enpre。

这是一个如何查询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_enpre($response);

您可以在此处看到响应类型为JSONObject

public CustomRequest(int method, String url,Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener)

看一下听众的参数!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412057.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号