Qamar Din February 2016

Fetch data in Json array and send it to server

I am developing an android application where I am fetch all the data in j son array. Now i want to send said Json array to MySQL database on server.

db.open();     
ArrayList<HashMap<String, String>> arrayList = db.Record();    
Gson gson = new Gson();   
String jsonPesan = gson.toJson(arrayList);  
db.close();

Answers


Vasant February 2016

You need to make web-service ,you can post your data to server using webservice.

How to send data to MySQL DB without using JDBC , PHP or any other webservice?


Aziz February 2016

You can use JSON API to create a json array and then use URL Builder to send the json as a parameter to the API.

jsonAttriArray = new JSONArray();
                                try {
                                    if (!someInput.getText().toString().equalsIgnoreCase(""))
                                             {
                                        JSONObject jsonObject1 = new JSONObject();
                                        jsonObject1.put("object1", someInput.getText());

                                        jsonAttriArray.put(jsonObject1);
                                    }
                                    if (!someInput2.getText().toString().equalsIgnoreCase(""))
                                             {
                                        JSONObject jsonObject2 = new JSONObject();
                                        jsonObject2.put("object2", someInput2.getText());

                                        jsonAttriArray.put(jsonObject2);

                                    }

                                } catch (Exception e) {
                                    // TO DO
                                }

Uri.Builder builder = new Uri.Builder(); builder.appendQueryParameter("jsonparams", jsonAttriArray.toString());

 String query = builder.build().getEncodedQuery();
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();

Post Status

Asked in February 2016
Viewed 2,928 times
Voted 10
Answered 2 times

Search




Leave an answer