Skip to content Skip to sidebar Skip to footer

Tutorial To Send Data As Jsonarray To Mysql In Android Using Php

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. I have a json in android : [ { 'id': '1', 'nilai': '1' },

Solution 1:

use volley library with a php file in server.

Here is a helpful links:

android developers volley

Volley json post tuturial

Solution 2:

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"}]

what you can do is :

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}");
}

then use Volley to send the ArrayList with POST:

String url = "url that handle the request";
    final Context currentContext = CompleteDataActivity.this;
    final StringRequest request = newStringRequest(Request.Method.POST, url,
            newResponse.Listener<String>() {
                @OverridepublicvoidonResponse(String response) {
                    // success handling code here
            }, newResponse.ErrorListener() {
        @OverridepublicvoidonErrorResponse(VolleyError error) {
            // error handling code here
        }
    }){
        @OverrideprotectedMap<String, String> getParams() throws AuthFailureError {
            Map<String, String> param = newHashMap<>();
            //Post parameterfor(int i=0;i<yourList.size();i++)
            {
                param.put("jsonObject"+i, yourList.get(i));
            }

            return param;
        }
    };
    H.showLoadingDialog(currentContext);
    MyVolley.getRequestQueue().add(request);

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);
        }

hope this helps

Post a Comment for "Tutorial To Send Data As Jsonarray To Mysql In Android Using Php"