I need to send a JSON
string to a url
using POST
.
the string should be:
data={"cmd":"sign_in",....}
So in doInBackground
, I used this code:
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String json = "{\"cmd\": \"sign_in\",...";
try{
URL url = new URL("https://...?data=");
connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("charset", "utf-8");
connection.connect();
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write(json);
osw.flush();
osw.close();
}
I check my response using
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
if(serverResponseMessage.length()>0)
return true;
But I am not receiving any response. The response string should be a JSON
string. But I used length()
to see whether any string is being returned.
Should this code work? Any help?