I want to send multiple values in one key, like sendTo[] -string array and values comma separated in that like abcd@gmail.com,pqrs@gmail.com.
This I tried to achieve using string builder, appended all values using comma separated and then add the builder in hash map with the key.
Like this :
StringBuilder emailbuilder = new StringBuilder();
for (String mail : mEmailList) {
emailbuilder.append(mail+",");
i++;
}
data.put("send_to[]", emailbuilder.toString());
So it results in hashmap as abcd@gmail.com,pqrs@gmail.com, -- string like this gets added as value for key send_to[].
Later, the type of data sending is x-www-form-urlencoded, so I am converting data into that format.
function to convert :
private String getDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
But, after converting it converts the values too. The send_to[] key , value becomes like ---&send_to%5B%5D=realdammy%40hotmail.com%2Csiddhijambhale%40gmail.com%2C
It converts brackets and comma into % format. I need to send data like --- send_to key and value abcd@gmail.com,pqrs@gmail.com
How can I achieve this? Please help Thank you...
EDIT: The function to run the request.
public String sendPostRequest(String data) {
try {
URL url = new URL(api);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("content-type", "application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setDoInput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
try {
writer.write(data);
} catch (Exception e) {
Log.e("Exception111", e.toString());
}
writer.close();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
Log.d("ServerResponse", new String(sb));
String output = new String(sb);
return output;
} else {
Log.e("Exception", "" + responseCode);
}
}
catch(IOException e)
{
Log.e("Exception", "" + e.toString());
}
return null;
}