通过JSON发送多个记录PHP的MySQL [英] Send multiple records via json to php mysql
本文介绍了通过JSON发送多个记录PHP的MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在同一时间使用JSON发送多个记录?这code是一个示例我在网上找到,但我需要发送100说对象或记录在同一时间。该数据来自一个数据库。
保护字符串doInBackground(字符串参数... args){
字符串名称= inputName.getText()的toString()。
串价格= inputPrice.getText()的toString()。
。字符串描述= inputDesc.getText()的toString(); //大厦参数
清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
params.add(新BasicNameValuePair(姓名,名字));
params.add(新BasicNameValuePair(价格,价格));
params.add(新BasicNameValuePair(说明,说明)); //获取JSON对象
//注意,创建产品网址接受POST方法
JSONObject的JSON = jsonParser.makeHtt prequest(url_create_product,
POST,则params);
解决方案
您必须创建的JSONObject的重新present的对象,并将其添加到请求:
如果你有这样的结构,例如:
{[{名:名称1,价格:10},{名字:NAME2,价格:15}]}
JSONArray元素=新JSONArray();
JSONObject的辅助=新的JSONObject()把(名,名1);
aux.put(价格,10);
array.put(辅助); AUX =新的JSONObject()把(「NAME1,NAME2);
aux.put(价格,20);
array.put(辅助);清单<&的NameValuePair GT;参数=新的ArrayList<&的NameValuePair GT;();
parameters.add(新BasicNameValuePair(JSON,elements.toString()));
HttpPost后=新HttpPost(URL);
post.setEntity(新UrlEn codedFormEntity(对));
HttpClient的cliente = createHttpClient();
返回cliente.execute(岗位);
和服务器你赶上参数JSON
How do I send more than one record at a time using json? This code is a sample I found online but I need to send say 100 objects or records at a time. The data comes from a database.
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);
解决方案
You must create the JSONObject that represent the objects, and add it to the Request:
If you have this structure for example:
{[{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);
And in the server you catch the parameter "json"
这篇关于通过JSON发送多个记录PHP的MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文