【问题标题】:Send multiple records via json to php mysql通过json发送多条记录到php mysql
【发布时间】:2023-03-15 09:01:01
【问题描述】:

如何使用 json 一次发送多个记录?此代码是我在网上找到的示例,但我需要一次发送 100 个对象或记录。数据来自数据库。

protected String doInBackground(String... args) {
        String name = inputName.getText().toString();
        String price = inputPrice.getText().toString();
        String description = inputDesc.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("price", price));
        params.add(new BasicNameValuePair("description", description));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);

【问题讨论】:

  • 使用一个循环并发送 100 个怎么样?
  • 你实际上并没有发送 json,你只是在接收它。为了发送多个项目,您需要知道服务器如何期望多个项目。目前,除了遵循@ns47731 的建议之外,似乎没有其他方法可以做到这一点。
  • 我创建了一个循环但是服务器接收到的数据不一致。接收方的记录丢失。 android部分还可以。有人给了我创建对象并将它们添加到数组中的想法。它工作得很棒,而且在服务器端运行得更快。
  • 我能够使用数组中的 jsonobjects 发送 3800 个数据库行。现在我只需要完善循环。

标签: java php android sql json


【解决方案1】:

您必须创建代表对象的 JSONObject,并将其添加到请求中:

如果你有这样的结构,例如:

{[{name:"name1",price:"10"},{name:"name2",price:"15"}]}

 JSONArray elements=new JSONArray();
  JSONObject aux=new JSONObject().put("name", "name1");
    aux.put("price", 10);
    array.put(aux);

    aux=new JSONObject().put("name1", "name2");
    aux.put("price", 20);
    array.put(aux);

List<NameValuePair> parameters= new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("json", elements.toString()));
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpClient cliente = createHttpClient();
return cliente.execute(post);

然后在服务器中捕获参数“json”

【讨论】:

  • 这篇文章对我的理解最大!我能够掌握制作一个对象并将其放入数组的想法。制作另一个对象并将其添加到数组等......等等......
【解决方案2】:

您可以将数据列表写入String,然后将其发送到php url。在phpjson_decode中读取数据列表;

public static class Entity{
        private String name;
        private String price;
        private String description;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

    @Test
    public void send() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        List<Entity> list = new ArrayList<Entity>(); // get the list of Entity;
        String json = mapper.writeValueAsString(list); // write list as json

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://url.to.post");

        StringEntity entity = new StringEntity(json);
        post.setEntity(entity);

        HttpResponse response = client.execute(post);

        String result = EntityUtils.toString(response.getEntity());
        Object responseObject = mapper.readValue(result, Object.class);
    }

为了使用 ObjectMapper,您的库中需要 jackson-core-asl 和 jackson-mapper-asl。

【讨论】:

  • 感谢您的帮助! ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
  • 2015-07-30
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
相关资源
最近更新 更多