我正在尝试在数据库中添加多个记录我正在用android中的string-request方法向php脚本发送json数组来添加这些记录,但是没有得到php中json数组的结果。我只想在mysql中添加多个记录,这就是为什么要向脚本发送json数组,通过获取其中的所有数据来添加这些记录
这里是插入数据函数
private void InsertData() {
if(arrayList.size()>0) {
Toast.makeText(getApplicationContext(), "list not null "+i+arrayList.size(),
Toast.LENGTH_LONG).show();
//for (i=0;i<arrayList.size();i++) {
//Toast.makeText(getApplicationContext(),"inside for loop", Toast.LENGTH_LONG).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Constants.insert_macthes,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getInt("success") == 1) {
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
} else
Toast.makeText(getApplicationContext(), "not added", Toast.LENGTH_LONG).show();
dis = jsonObject.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Toast.makeText(MyMatchas.this,
"Error time out",
Toast.LENGTH_LONG).show();
} else if (error instanceof AuthFailureError) {
//TODO
Toast.makeText(MyMatchas.this,
"Auth Error time out",
Toast.LENGTH_LONG).show();
} else if (error instanceof ServerError) {
//TODO
Toast.makeText(MyMatchas.this,
"Server Error time out",
Toast.LENGTH_LONG).show();
} else if (error instanceof NetworkError) {
//TODO
Toast.makeText(MyMatchas.this,
" Network Error time out",
Toast.LENGTH_LONG).show();
} else if (error instanceof ParseError) {
Toast.makeText(MyMatchas.this,
"Parse Error time out",
Toast.LENGTH_LONG).show();
//TODO
}
}
}) {
@Override
protected Map<String, String> getParams() {
JSONArray array=new JSONArray();
JSONObject jsonObject=new JSONObject();
for(i=0;i<arrayList.size();i++)
{
Log.v("fetching record:",arrayList.get(i).getId());
arr[i]="dog_id"+arrayList.get(i).getId()+"dog_name"+arrayList.get(i).getDog_name()+"score"+arrayList.get(i).getDog_score()+"user_id"+ String.valueOf(SharedPrefManager.getInstance(MyMatchas.this).getUserid());
try {
//Log.v("put in json:",arrayList.get(i).getId());
jsonObject.put("dog_id",arrayList.get(i).getId());
jsonObject.put("dog_name",arrayList.get(i).getDog_name());
jsonObject.put("score",arrayList.get(i).getDog_score());
jsonObject.put("user_id",String.valueOf(SharedPrefManager.getInstance(MyMatchas.this).getUserid()));
} catch (JSONException e) {
Log.v("error exceptiion:",arrayList.get(i).getId());
e.printStackTrace();
}
array.put(jsonObject);
}
HashMap<String ,String> params=new HashMap<String, String>();
Log.v("All data:",jsonObject.toString());
Log.v("json object data:",array.toString());
params.put("params",array.toString());
return params;
}
};
// Creating RequestQueue.
RequestQueue requestQueue = Volley.newRequestQueue(MyMatchas.this);
// Adding the StringRequest object into requestQueue.
requestQueue.add(stringRequest);
// }
// Toast.makeText(getApplicationContext(),dis,Toast.LENGTH_LONG).show();
}
下面是php脚本
<?php
$con=mysqli_connect("localhost","m","rt","Friend");
if($_SERVER['REQUEST_METHOD']=='POST'){
$arr = $_POST['params'];
$json = json_decode($arr,true);
echo $json;
foreach($json as $obj){
$Sql_Query = "INSERT INTO Match_list (dog_id,score,dog_name,User_ID) values
('$obj->dog_id','$obj->dog_name','$obj->score','$obj->user_id')";
if($con->query($Sql_Query) === TRUE)
{
die(json_encode(array("success"=>1,"message"=>"Data Added
Successfuly")));
}
}
}
mysqli_close($con);
?>
2条答案
按热度按时间9vw9lbht1#
您需要为每次保存新值创建新对象
pvabu6sv2#
您可以访问如下值: