1

I am unable to send JSONArray string to php server using multipart entity in android. I have tried following but it's not working:

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset());
entity.addPart("invite_friend", new StringBody(friendsArray));

In PHP server side it should come like:

'invite_friend' => array
    (
        0 => '800'
        1 => '794'
    )

What could be done, please provide suggestions.

Rajeev Sahu
  • 1,732
  • 6
  • 26
  • 39

2 Answers2

1

You can do something like this.

// Prepare Category Array
for (String mFrndsID : friendsArray) {
    reqEntity.addPart("invite_friend[]", new StringBody(mFrndsID));
}

Just add [] with you array tag and paas values in a loop in it. Here invite_friend is the array tag. You can pass your values in this tag by using loop. it will post as an array on server.

Refer This Answer for more detail

How to send the string array of values in one key word using post method to the server

this may help you

Community
  • 1
  • 1
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

Try this code for sending multiple value pairs to server side scripts.

String strUrl = "http://****/****.php";
url = new URL(strUrl);

HttpURLConnection connection = (HttpURLConnection) url
        .openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
        connection.getOutputStream());

outputStreamWriter.write("user_names_giver=" +user_names_giver + "&tcredit="+tcredit+"&user_name_receiver="+user_name_receiver);                
outputStreamWriter.flush();
outputStreamWriter.close();

InputStream iStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(iStream));

StringBuffer sb = new StringBuffer();

String line = "";

while( (line = reader.readLine()) != null)
{
    sb.append(line);
}

reader.close();
iStream.close();
tomrozb
  • 25,773
  • 31
  • 101
  • 122
Naren
  • 1
  • 6