Java 通过json将多个记录发送到php mysql
如何使用json一次发送多条记录?这段代码是我在网上找到的一个示例,但我需要一次发送100个对象或记录。数据来自数据库Java 通过json将多个记录发送到php mysql,java,php,android,sql,json,Java,Php,Android,Sql,Json,如何使用json一次发送多条记录?这段代码是我在网上找到的一个示例,但我需要一次发送100个对象或记录。数据来自数据库 protected String doInBackground(String... args) { String name = inputName.getText().toString(); String price = inputPrice.getText().toString(); String description = i
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);
受保护的字符串doInBackground(字符串…args){
字符串名称=inputName.getText().toString();
字符串价格=inputPrice.getText().toString();
字符串描述=inputDesc.getText().toString();
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“名称”),名称);
参数添加(新的BasicNameValuePair(“价格”,价格));
参数添加(新的BasicNameValuePair(“说明”,说明));
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=jsonParser.makeHttpRequest(url\u create\u product,
“POST”,params);
您可以将数据列表写入字符串,然后将其发送到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);
}
公共静态类实体{
私有字符串名称;
私人字符串价格;
私有字符串描述;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getPrice(){
退货价格;
}
公共无效设置价格(字符串价格){
这个价格=价格;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
}
@试验
public void send()引发异常{
ObjectMapper mapper=新的ObjectMapper();
List List=new ArrayList();//获取实体列表;
String json=mapper.writeValueAsString(list);//将列表写为json
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(“http://url.to.post");
StringEntity=新的StringEntity(json);
后设实体(实体);
HttpResponse response=client.execute(post);
字符串结果=EntityUtils.toString(response.getEntity());
Object responseObject=mapper.readValue(结果,Object.class);
}
要使用ObjectMapper,您需要在LIB中使用jackson core asl和jackson mapper asl。您必须创建表示对象的JSONObject,并将其添加到请求中: 例如,如果您有此结构: {[{name:“name1”,price:“10”},{name:“name2”,price:“15”}
JSONArray元素=新的JSONArray();
JSONObject aux=newJSONObject().put(“name”,“name1”);
辅助卖出价(“价格”,第10页);
数组.put(aux);
aux=newJSONObject().put(“name1”、“name2”);
辅助卖出价(“价格”,20);
数组.put(aux);
列表参数=新的ArrayList();
add(新的BasicNameValuePair(“json”,elements.toString());
HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(对));
HttpClient客户端=createHttpClient();
返回客户。执行(post);
在服务器中捕获参数“json”使用一个循环并发送100个循环怎么样?实际上,你不是在发送json,你只是在接收它。为了发送多个项目,你需要知道服务器是如何期望多个项目的。目前,除了遵循@ns47731的建议外,似乎没有其他方法可以做到这一点。我创建了一个循环,但数据接收到了服务器发送的数据不一致。接收端缺少记录。android部分还可以。有人给了我创建对象并将它们添加到数组中的想法。在服务器端,这非常有效,而且速度更快。我能够在一个数组中使用JSONObject发送3800个数据库行。现在我只需完善循环。Th这篇文章帮助我理解得最多!我能够掌握制作一个对象并将其放入数组的想法。制作另一个对象并将其添加到数组中等等…等等…谢谢你的帮助!;)
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);