【问题标题】:Sending an array to php using retrofit使用改造将数组发送到 php
【发布时间】:2020-05-09 15:43:15
【问题描述】:

在我的 android 应用程序中,我正在使用改造 1.9 使用 mysql 获取一些数据。我获取的数据在 json 数组中。现在我想一次在我的 sql 查询中使用数组中的所有值。例如,这是我从 mysql 获取的 json 数组

[{"receivers":"Can23584PtqA"},{"receivers":"New565159nrsN"},{"receivers":"NY H3V9tcig"}]

你可以有三个值。值可以是“n”。数组长度没有限制,但现在我想使用改造 1.9 将所有这些再次发送到我的 sql。如果我使用循环,那么每次只有 1 个值会转到 php。有什么方法可以发送整个数组并使用 mysql 查询中的所有值

    JSONArray jsonArray = new JSONArray(output);
  for (int i = 0; i < jsonArray.length(); i++) {


                                    JSONObject jsonObject = jsonArray.getJSONObject(i);//returnJson;

                                    String receiver=jsonObject.getString("receivers");// this is the string now i can send it to php but problem is this only one value will be sent at a time. I need all values at same in my sql query



                                            }

【问题讨论】:

  • 请向我们展示您的尝试。另外,您的实际问题与 PHP 和/或 mysql 有关吗?您应该只添加与问题相关的标签。
  • 您想将数据发送到哪里?在服务器或本地 sql 表上?

标签: android arrays retrofit


【解决方案1】:
 JSONArray jsonArray = new JSONArray(output);
    //list to store list of all receivers
    ArrayList<String> receivers=new ArrayList<>();

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = null;//returnJson;
        try {
            jsonObject = jsonArray.getJSONObject(i);
            String receiver=jsonObject.getString("receivers");
            receivers.add(receiver);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    //now pass this 'receivers' list to your php server via web api
    //@FormUrlEncoded
    //@POST("index.php?action=item")
    //Call<Receiver> sendReceivers(@Field("items[]") List<String> items);

【讨论】:

  • 我将如何在 php 中获取 item[] 数组
  • 你能告诉我在php参数中获取列表数组的语法吗
【解决方案2】:

如果您想再次发送到 php,您可以使用 JSON 或 Gson 进行发送。我推荐给Gson。

例如:

Gson gson = new Gson();
ArrayList<Object> array; // Your array recieved from server it will be object array
String stringarray = gson.toJson(array); //So this is a complete array in single string value you can de-serialize it on both php and android.

如果对你有帮助,请告诉我

【讨论】:

    猜你喜欢
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多