Android HTTPUrlConnection : how to set post data in http body?

If you want to send String only try this way:

String str =  "some string goes here";
byte[] outputInBytes = str.getBytes("UTF-8");
OutputStream os = conn.getOutputStream();
os.write( outputInBytes );    
os.close();

But if you want to send as Json change Content type to:

conn.setRequestProperty("Content-Type","application/json");  

and now our str we can write:

String str =  "{\"x\": \"val1\",\"y\":\"val2\"}";

Hope it will help,

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)