Android Volley - POST参数未达到PHP脚本

时间:2017-03-08 11:44:32

标签: java php android

我想从数据库中获取信息。当我使用不带参数的查询时(例如"从用户中选择*;"我的脚本运行正常。

现在我有以下脚本返回一个空数组。当我用脚本值替换脚本中的$ groupId(例如" Group1")时,它返回预期的Items。

<?php


$sql = "";
if($_SERVER['REQUEST_METHOD']== 'POST') {
$flag = array();

$groupId = $_POST['groupId'];



require_once('dbConnect.php');

$sql = "SELECT * FROM users where groupId = '$groupId'";

 if($out = mysqli_query($con,$sql)){
 //echo "Successfully Registered";

    while($row = mysqli_fetch_array($out))
    {

        $flag[] = $row;

    }
    print(json_encode($flag));

 }else{
 echo "Could not register";

 }
 }else{
  echo 'error';
}


//mysqli_close($con);
?>

时,我使用hashmap hashMapGetNames.put("groupId", "Group#1");

调用以下函数

然后我得到一个空数组和信息:&#34;不,组ID未设置&#34;

public ArrayList<String> get(String url,  final HashMap<String, String> hashMap,  final Context context) {


        //Download the items from DB
        final ArrayList<String> items = new ArrayList<>();
        RequestQueue queue = Volley.newRequestQueue(context);
        JSONObject parameters = new JSONObject(hashMap);
        final JsonRequest<JSONArray> jsonArrayRequest = new
                JsonRequest<JSONArray>(Request.Method.GET, url, parameters.toString(),
                new com.android.volley.Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray jsonArray) {
                        if(jsonArray == null){
                            Log.d("Downloader", "FAIL: NO NAMES FOUND");
                        }
                        else {
                            for (int zähler = 0; zähler < jsonArray.length(); zähler++){
                                try {
                                    Log.d("Downloader", "sind bei zähler " + zähler);
                                    JSONObject object = jsonArray.getJSONObject(zähler);
                                    String name = object.getString("name");
                                    Log.d("Name", name);
                                    items.add(name);
                                }
                             catch (JSONException e) {
                                 Log.d("Downloader", "Catching exception");
                                e.printStackTrace();
                            }
                            }
                        }



                    }
                }, new com.android.volley.Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Downloader", "Error: " + error.toString());

            }

        })

                {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Log.d("Downloader", "gettingParams");
                        return hashMap;
                    }

                    @Override
                    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
                            Log.d("Downloader", "parsing Network response");
                        try {
                            String jsonString = new String(response.data,
                                    HttpHeaderParser
                                            .parseCharset(response.headers));
                            Log.d("Downloader", "Parsing success");
                            return Response.success(new JSONArray(jsonString),
                                    HttpHeaderParser
                                            .parseCacheHeaders(response));
                        } catch (UnsupportedEncodingException e) {
                            Log.d("Downloader", e.toString());
                            return Response.error(new ParseError(e));

                        } catch (JSONException je) {
                            Log.d("Downloader", je.toString());
                            return Response.error(new ParseError(je));
                        }
                    }
                };

        queue.add(jsonArrayRequest);

        return items;

    }

所以脚本似乎没错,但参数没有到达脚本。错误可能隐藏的任何想法?

编辑:我编辑了下面评论的教程中显示的代码,并使用相同的dbConnect.php文件。但它打印出错误 - 所以问题仍然存在。这意味着请求方法不是&#34; Post&#34;。 Android Monitor打印出来:

  

解析网络响应

     

下载程序:解析成功

     

错误:com.android.volley.ParseError:org.json.JSONException:字符0处的输入结束

1 个答案:

答案 0 :(得分:1)

试试这段代码......

         @Override
        protected Map<String, String> getParams() throws AuthFailureError {


            //Creating parameters
            Map<String,String> params = new Hashtable<String, String>();

            //Adding parameters
            params.put("groupId", "122");


            //returning parameters
            return params;
        }