0

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"
  },
  {
    "id": "2",
    "nilai": "1"
  },
  {
    "id": "3",
    "nilai": "1"
  },
  {
    "id": "4",
    "nilai": "1"
  },
  {
    "id": "5",
    "nilai": "1"
  }
]

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

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

0

2 Answers 2

0

use volley library with a php file in server.

Here is a helpful links:

android developers volley

Volley json post tuturial

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggest sir, but i have read the that tutor last time, and i dont know how it work with my project, because the tutor just insert a single row, and mine, i need to insert 5 data at once with button
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.