gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:44:09 27 4
gpt4 key购买 nike

我已经尝试使用普通的 JSONArrayRequestsStringRequests,直到现在一切都很好。我想发送一个带有 POST 参数的 JSONArrayRequest 以从脚本中获取一些 JSON 格式的 MySQL 结果。不幸的是,我每次都得到 [] 作为回应。我已经检查了 .php 文件并使用 _GET 方法进行查询,并且脚本完美地以 Json 格式返回所需的行。我在这里读到 (https://stackoverflow.com/a/18052417/4959185) Volley 队已经在他们的类(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 类中的静态字段,包​​含直接链接到我服务器上的 PHP 脚本 http://www.someserversite.com/my_api/gmy_php_script.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;
?>

关于java - Volley JSONArrayRequest - 没有正确发送参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33977451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com