Java 截击JSONArrayRequest-未正确发送参数?

Java 截击JSONArrayRequest-未正确发送参数?,java,android,post,android-volley,Java,Android,Post,Android Volley,我试过使用普通的JSONArrayRequests和StringRequests,直到现在一切都很好。我想发送一个带有POST参数的JSONArrayRequest,从脚本中获取JSON格式的MySQL结果。不幸的是,我每次都得到[]的响应。我用\u GET方法检查了.php文件和查询,脚本以Json格式完美地返回了所需的行。 我在这里读到()截击队在他们的类中添加了带有\u POST参数的JSONArrayRequest。然而,它在我的情况下不起作用。请您看看该功能有什么问题: pri

我试过使用普通的
JSONArrayRequests
StringRequests
,直到现在一切都很好。我想发送一个带有POST参数的
JSONArrayRequest
,从脚本中获取JSON格式的MySQL结果。不幸的是,我每次都得到
[]
的响应。我用
\u GET
方法检查了.php文件和查询,脚本以
Json
格式完美地返回了所需的行。 我在这里读到()截击队在他们的类中添加了带有
\u 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);
}
private void getFavoriteRecipes(最终字符串userUniqueId,最终整数偏移量){
JsonArrayRequest FavoriteRecipeReq=新的JsonArrayRequest(Request.Method.POST,
AppConfig.URL_getFavoriteRecipes,新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
Log.d(“odpowiedz”,“Odpowiedźulubionych:+响应”);
对于(int i=0;i
我的PHP脚本:

我找到了另一种通过发送参数获取JSONArrayResponse的方法。我想这会有帮助的

您只需编写标准JSONArrayRequest liek:

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);
正如你所看到的,这很简单。我得到了标准的
AppConfig.URL\u getFavoriteRecipes
,这是AppConfig类中的静态字段,可以直接链接到我的serwer f.e
http://www.someserversite.com/my_api/gmy_php_script.php
并将其与我需要发送到脚本的参数值结合起来:
user\u unique\u id
及其内容
userUniqueId
offset
哪个内容是从
int
解析到
字符串的
offset

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

<?php
// some code

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

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


自从您这样发送请求以来,您是否真的使用
POST
测试了您的脚本?我只使用_GET方法测试了我的php脚本,它运行良好。为什么它不应该与_POST一起工作?只是不同类型的发送参数。我不熟悉你的脚本,我只是想知道你为什么不用POST测试它,看看脚本是否正确返回。如果是的话,那么你肯定问题出在客户端(Android)。我在编辑中添加了链接。请检查:)
<?php
// some code

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

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