1

I've tried with normal JSONArrayRequests and StringRequests and everything was fine untill now. I want to send an JSONArrayRequest with POST parameters to get some MySQL result in JSON format from the script. Unfortunately I get [] everytime in response. I have checked .php file and query with _GET method and the script worked perfectly returning desired rows in Json format. I read here (https://stackoverflow.com/a/18052417/4959185) Volley Team have added JSONArrayRequest with _POST parameter to their class. However it does not work in my case. Could you please look what is wrong with that function:

    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);
}

My PHP Script: https://ideone.com/ZxYzHr

Community
  • 1
  • 1
anton86993
  • 638
  • 1
  • 9
  • 26
  • Have you actually tested your script with `POST` since you send the request like that ? – Andrei Catinean Nov 28 '15 at 23:06
  • I have tested my php script only with _GET method and it worked well. Why it shouldn't work with _POST? Just different type of sending parameters. – anton86993 Nov 28 '15 at 23:10
  • I'm not familiar with your script, I was just wondering why you're not testing it with POST as well to see if the script returns correctly. If it does, then you're sure that the problem is on the client side (Android). – Andrei Catinean Nov 28 '15 at 23:12
  • I have added link to it in my edit. Pls check it :) – anton86993 Nov 28 '15 at 23:16

1 Answers1

1

I have found another way to get JSONArrayResponse with sending parameters. I think that will help somebody.

U just write standard JSONArrayRequest liek this:

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);

Instead of standard URL to the PHP script I inserted function returning String called prepareGetMethodUrl().

Let's look inside it:

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

As you can see it's very simple. I get standard AppConfig.URL_GETFAVOURITERECIPES which is static field in AppConfig class conatining direct link to my PHP script on my serwer f.e http://www.someserversite.com/my_api/gmy_php_script.php and combine it with parametres values I need to send to the script: user_unique_id and it's content userUniqueId and offset which content is offset parsed from int to String.

Inside my script I just call:

<?php
// some code

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

echo $user_unique_id . "<br />";
echo $offset;
?>
anton86993
  • 638
  • 1
  • 9
  • 26