Volley JSONArrayRequest - 没有正确发送参数?

Volley JSONArrayRequest - not sending params properly?

我已经尝试使用正常的 JSONArrayRequestsStringRequests,直到现在一切都很好。我想发送一个带有 POST 参数的 JSONArrayRequest 以从脚本中以 JSON 格式获得一些 MySQL 结果。不幸的是,我每次都得到 [] 作为回应。我已经检查了 .php 文件并使用 _GET 方法进行查询,并且脚本完美地以 Json 格式返回了所需的行。 我在这里阅读 () Volley Team 在他们的 class 中添加了带有 _POST 参数的 JSONArrayRequest。但是它在我的情况下不起作用。你能看看那个函数有什么问题吗:

    private void getFavouriteRecipes(final String userUniqueId, final int offset) {

    JsonArrayRequest favouriteRecipesReq = new JsonArrayRequest(Request.Method.POST,
            AppConfig.URL_GETFAVOURITERECIPES, new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    Log.d("odpowiedz", "Odpowiedź ulubionych: " + response);

                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject jObj = response.getJSONObject(i);
                            RecipeItem recipeItem = new RecipeItem();
                            recipeItem.setRecipeUniqueID(jObj.getString("unique_id"));
                            recipeItem.setRecipeTitle(jObj.getString("title"));
                            recipeItem.setRecipeImgThumbnailLink(jObj.getString(
                                    "img_tumbnail_link"));
                            recipeItem.setRecipeAddAte(jObj.getString("add_date"));
                            recipeItem.setRecipeKitchenType(jObj.getString("kitchen_type"));
                            recipeItem.setRecipeMealType(jObj.getString("meal_type"));
                            recipeItem.setRecipeName(jObj.getString("name"));
                            recipeItem.setRecipeSurname(jObj.getString("surname"));
                            recipeItem.setRecipeLikeCount(jObj.getString("like_count"));
                            recipeFavouriteItems.add(recipeItem);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            showSnackbarInfo("Błąd Json: " + e.getMessage(),
                                    R.color.snackbar_error_msg);
                        }
                    }
                    recipeFavouriteItemsAdapter.notifyDataSetChanged();
                }

            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("odpowiedz", "Błąd pobierania ulubionych: " +
                            Integer.toString(error.networkResponse.statusCode));

                    showSnackbarInfo(Integer.toString(error.networkResponse.statusCode),
                            R.color.snackbar_error_msg);
                }

            }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting Parameters to Login URL
            Map<String, String> params = new HashMap<>();
            params.put("user_unique_id", userUniqueId);
            params.put("offset", Integer.toString(offset));

            Log.d(TAG, "wysylam parametry: " + userUniqueId + ", " + Integer.toString(offset));
            return params;
        }
    };

    // Adding Request to Request Queue
    AppController.getInstance().addToRequestQueue(favouriteRecipesReq);
}

我的PHP脚本: https://ideone.com/ZxYzHr

我找到了另一种通过发送参数获取 JSONArrayResponse 的方法。我认为这会对某些人有所帮助。

你只需编写标准的 JSONArrayRequest 如下:

JsonArrayRequest favouriteRecipesReq = new JsonArrayRequest(prepareGetMethodUrl(),
                new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d("odpowiedz", "Odpowiedź ulubionych: " + response.toString());

                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject jObj = response.getJSONObject(i);
                                RecipeItem recipeItem = new RecipeItem();
                                recipeItem.setRecipeUniqueID(jObj.getString("unique_id"));
                                recipeItem.setRecipeTitle(jObj.getString("title"));
                                recipeItem.setRecipeImgThumbnailLink(jObj.getString(
                                        "img_tumbnail_link"));
                                recipeItem.setRecipeAddAte(jObj.getString("add_date"));
                                recipeItem.setRecipeKitchenType(jObj.getString("kitchen_type"));
                                recipeItem.setRecipeMealType(jObj.getString("meal_type"));
                                recipeItem.setRecipeName(jObj.getString("name"));
                                recipeItem.setRecipeSurname(jObj.getString("surname"));
                                recipeItem.setRecipeLikeCount(jObj.getString("like_count"));
                                recipeFavouriteItems.add(recipeItem);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        recipeFavouriteItemsAdapter.notifyDataSetChanged();
                    }

                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("odpowiedz", "Błąd pobierania ulubionych: " +
                                Integer.toString(error.networkResponse.statusCode));

                        showSnackbarInfo(Integer.toString(error.networkResponse.statusCode),
                                R.color.snackbar_error_msg);
                    }

                });

        // Adding Request to Request Queue
        AppController.getInstance().addToRequestQueue(favouriteRecipesReq);

我插入了返回 String 的函数,而不是 PHP 脚本的标准 URL 调用 prepareGetMethodUrl().

让我们看看里面:

private String prepareGetMethodUrl() {
    return AppConfig.URL_GETFAVOURITERECIPES + "?user_unique_id=" + userUniqueId + "&offset=" +
            Integer.toString(offset);
}

如您所见,它非常简单。我得到标准 AppConfig.URL_GETFAVOURITERECIPES,它是 AppConfig class 中的静态字段,将直接 link 连接到我服务器 f.e http://www.someserversite.com/my_api/gmy_php_script.php 上的 PHP 脚本并将其组合使用我需要发送到脚本的参数值:user_unique_id 及其内容 userUniqueIdoffset 内容 offsetint 解析为 String.

在我的脚本中,我只是调用:

<?php
// some code

// Receiving The Post Params
$user_unique_id = $_GET['user_unique_id'];
$offset = $_GET['offset'];

echo $user_unique_id . "<br />";
echo $offset;
?>