使用PHP将数据作为JsonArray发送到Android中的MySQL的教程 [英] Tutorial to send data as JsonArray to MySQL in Android using PHP
问题描述
我已经在g * ogle中尝试了几天,以找到任何好的教程.
I've tried a couple days in g*ogle to find any good tutorial how to do.
但是直到现在,我还没有找到.
But until now, I have not find yet.
我在android中有一个json:
I have a json in android :
[
{
"id": "1",
"nilai": "1"
},
{
"id": "2",
"nilai": "1"
},
{
"id": "3",
"nilai": "1"
},
{
"id": "4",
"nilai": "1"
},
{
"id": "5",
"nilai": "1"
}
]
然后将String在id
和nilai
中循环五次,然后我想在按下按钮时将此json发送到服务器.
And the String is looped the id
and nilai
for five times, then I want to send this json to server when button pressed.
我要存储的数据库如下:
The database i want to store looks like :
|---|-----|
| ID|Nilai|
|---|-----|
| 1 | 1 |
| 2 | 1 |
|...| ... |
有什么建议或想法吗? 我不介意您是否帮助回答应用程序中提供的代码,至少该代码可以为我提供帮助
Any suggest or idea? I do not mind if you help answer with a code that you have on your application, at least the code can help me
由于我自己对android和php编程的了解有限,我无法尝试执行该代码,但是当我有参考或类似案例的示例时,可以尝试该代码
I could not try the code how to do, due to limited understanding of the android and php programming myself, but I can try the code when I have a reference or examples of similar cases
推荐答案
您要发送类似[{"id":"1","nilai":"1"},{"id":"2 ," nilai:" 1},{" id:" 3," nilai:" 1},{" id:" 4," nilai:" 1},{" id :" 5," nilai:" 1}]
you want to send something like that [{"id":"1","nilai":"1"},{"id":"2","nilai":"1"},{"id":"3","nilai":"1"},{"id":"4","nilai":"1"},{"id":"5","nilai":"1"}]
您可以做的是:
Java/Android
Java/Android
String jsonObjectString="{\"id\":1,\"nilai\":1}";
您可以使用循环来制作所需的东西:
you can use a loop to make as much as you want:
ArrayList<String> yourList=new Arrayist<>();
for(int i=0;i<5;i++)
{
yourList.add("{\"id\":"+(i+1)+",\"nilai\":1}");
}
然后使用Volley通过POST发送ArrayList:
then use Volley to send the ArrayList with POST:
String url = "url that handle the request";
final Context currentContext = CompleteDataActivity.this;
final StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// success handling code here
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error handling code here
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> param = new HashMap<>();
//Post parameter
for(int i=0;i<yourList.size();i++)
{
param.put("jsonObject"+i, yourList.get(i));
}
return param;
}
};
H.showLoadingDialog(currentContext);
MyVolley.getRequestQueue().add(request);
使用此代码获取.php的JSONObject列表
use this code to get your list of JSONObject for .php
for($i=0;$i<5;$i++)
{
$var = "jsonObject".$i;
$jsonObject=json_decode($_POST[$var]);
$id = $jsonEtape->{'id'};
$nilai = $jsonEtape->{'nilai'};
$sql = "your query;";
mysqli_query($con,$sql);
}
希望这会有所帮助
这篇关于使用PHP将数据作为JsonArray发送到Android中的MySQL的教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!