如何从ANDROID - > PHP - > MySQL中的jsonArray中发送多个数据?

Is there a way to send data like the title said?

I know how to send data from TextView or EditText, I just have to insert a single data to the server, but to send data from JsonArray that has multiple data? I don't know how to make it work.

I created a JsonArray with this code in button.onClick and set it to TextView so I know the output of JsonArray:

int size = adapter3.getCount();
StringBuilder b = new StringBuilder();
JSONArray jArray = new JSONArray();
for (int i=0;i<=size;i++){
     jArray.put(adapter3.getItem(i).getJsonObject());
}
b.append(jArray);
TextView Star = (TextView)findViewById(R.id.test);
Star.setText(b.toString());

Then the output seems like:

[{"id":"1","nilai":"1"},{"id":"2","nilai":"1"},{"id":"3","nilai":"1"},{"id":"4","nilai":"1"},{"id":"5","nilai":"1"}]

Now I don't know how to send this JsonArray to MySQL through PHP, so I need help here.

full code :

MainActivity.java

public class MainActivity2 extends AppCompatActivity {

ListView listView;
ArrayList < Pertanyaan > listPertanyaan;
ArrayAdapter < Pertanyaan > adapter3;
Button kirim;
TextView dos;
TextView kul;
ProgressDialog pDialog;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tanya);
Intent intent = getIntent();
String id = intent.getStringExtra("dosen");
String mat = intent.getStringExtra("matkul");
dos = (TextView) findViewById(R.id.dosen);
kul = (TextView) findViewById(R.id.matakuliah);
dos.setText(id);
kul.setText(mat);
listView = (ListView) findViewById(R.id.list_view);
kirim = (Button) findViewById(R.id.finish);
listPertanyaan = new ArrayList < > ();
getpertanyaan get = new getpertanyaan();
get.execute();
adapter3 = new PertanyaanAdapter(this, R.layout.item_listview, listPertanyaan);
listView.setAdapter(adapter3);
kirim.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new CreateNewProduct().execute();
        int size = adapter3.getCount();
        StringBuilder b = new StringBuilder();
        JSONArray jArray = new JSONArray();
        for (int i = 0; i <= size; i++) {
            jArray.put(adapter3.getItem(i).getJsonObject());
        }
        b.append(jArray);
        TextView Star = (TextView) findViewById(R.id.test);
        Star.setText(b.toString());
    }
});
}

Pertanyaan.java

public class Pertanyaan {
    private String id;
    private float ratingStar;
    private String ask;

    Pertanyaan(String id, int ratingStar, String ask) {
        this.id = id;
        this.ratingStar = ratingStar;
        this.ask = ask;
    }

    public String getId() {
        return id;
    }

    float getRatingStar() {
        return ratingStar;
    }

    void setRatingStar(float ratingStar) {
        this.ratingStar = ratingStar;
    }

    public String getAsk() {
        return ask;
    }

    public JSONObject getJsonObject() {
        JSONObject obj = new JSONObject();
        try {
            obj.put("id", id);
            obj.put("nilai", ratingStar);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }
}

PertanyaanAdapter.java

class PertanyaanAdapter extends ArrayAdapter<Pertanyaan> {

    private AppCompatActivity activity;
    private List<Pertanyaan> movieList;

    PertanyaanAdapter(AppCompatActivity context, int resource, List<Pertanyaan> objects) {
        super(context, resource, objects);
        this.activity = context;
        this.movieList = objects;
    }

    @Override
    public Pertanyaan getItem(int position) {
        return movieList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_listview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
            //holder.ratingBar.getTag(position);
        }

        holder.ratingBar.setOnRatingBarChangeListener(onRatingChangedListener(position));

        holder.ratingBar.setTag(position);
        holder.ratingBar.setRating(getItem(position).getRatingStar());
        holder.movieName.setText(getItem(position).getAsk());
        holder.id.setText(getItem(position).getId());

        return convertView;
    }

    private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final int position) {
        return new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                Pertanyaan item = getItem(position);
                assert item != null;
                item.setRatingStar(v);
                Log.i("Adapter", "star: " + v);
            }
        };
    }

    private static class ViewHolder {
        private RatingBar ratingBar;
        private TextView movieName;
        private TextView id;

        ViewHolder(View view) {
            ratingBar = (RatingBar) view.findViewById(R.id.rate_img);
            movieName = (TextView) view.findViewById(R.id.text);
            id = (TextView) view.findViewById(R.id.textid);
        }
    }
}

To send post request use volley library :

compile 'dev.dworks.libs:volleyplus:+'

googling for use that library, this my code to send multiple post data

    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
            param.put("parameter1", jsonstring1);
            param.put("parameter2", jsonstring2);
            return param;
        }
    };
    H.showLoadingDialog(currentContext);
    MyVolley.getRequestQueue().add(request);

BTW, orang indo ya?

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