2

I have to pass JSONArray's object values to php which will then insert all data of object into mysql database. Below is my code in android,

MainActivity.java

private void uploadUserId_FriendId(String user_id , JSONArray friend_id) {
        try {
                try {


                    HttpClient httpClient = new DefaultHttpClient();

                    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));

                    HttpPost httpPost = new HttpPost("http://192.168.0.106/demo/demo.php");
                    MultipartEntity multipartEntity = new MultipartEntity();

                    multipartEntity.addPart("user_id", new StringBody(user_id));

// Here I have to add JsonArray object "friend_id", but it is not allowing me to add in //multipartentity

                    System.out.print("Test" + friend_id);
                   // JSONArray(friend_id);

                    httpPost.setEntity(multipartEntity);
                    HttpResponse httpResponse = httpClient.execute(httpPost);

                    if(httpResponse != null) {


                    } else { // Error, no response.
                        Toast.makeText(getBaseContext(),  "Server Error. ", 2000).show();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }

But here, I don't know how to add JSONArray object with MultipartEntity object, every time I try to add with MultipartEntity's object, it doesn't show any outputm. How can I add JSONArray data with MultipartEntity's object?

thumber nirmal
  • 1,639
  • 3
  • 16
  • 27

1 Answers1

0

In my case I'm putting the JSONArray inside an JSONObject and sending it. My setEntity and SetHeader code looks like this

    record.put("array", myJSONArray);
    httpPost.setEntity (new ByteArrayEntity (record.toString().getBytes ("UTF8")));
    httpPost.setHeader ("data", record.toString()); 

Here record is my JSONObject which contains my JSONArray.

SSS
  • 683
  • 1
  • 6
  • 16