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.

4
  • its not a JSONArray Commented Jun 18, 2014 at 6:07
  • refer this answer : stackoverflow.com/questions/18050533/… Commented Jun 18, 2014 at 6:09
  • that "friendsArray" contains like ["800","794"] in string value pair: "invite_friend":["800","794"]. How to send this "["800","794"]" data using Multipart. Commented Jun 18, 2014 at 6:17
  • its a StringArray...not JSONArray.. Commented Jun 18, 2014 at 6:28

2 Answers 2

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

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

Comments

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

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.