I am trying to add multiple records in database i am sending json array using string request method in volley in android to php script to add those records.But haven't getting the result of json array in php. I just want to add multiple records in mysql that why sending json array to script to add those records by fetching all data in it
Here is Insert Data function
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();
}
Here is the Php script
<?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);
?>
You need to create new object for every time for holding new values
JSONArray array = new JSONArray();
JSONObject jsonObject;
GettingVaccineData gettingVaccineData;
for (int i = 0; i < vaccineData.size(); i++) {
jsonObject = new JSONObject();
gettingVaccineData = vaccineData.get(i);
try {
jsonObject.put("LCID", reqResponse);
jsonObject.put("LVID", gettingVaccineData.getID());
jsonObject.put("LVNAME", gettingVaccineData.getNAME());
jsonObject.put("LSCHEDULE", scheduleDays.get(i));
jsonObject.put("LGIVEN", "NULL");
jsonObject.put("LSTATUS", "not given");
jsonObject.put("LHOSPITAL", "NULL");
} catch (JSONException e) {
e.printStackTrace();
}
array.put(jsonObject);
您可以访问如下值:
echo $json[0]["dog_id"];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.