Sending a JSON HTTP POST request from Android
Posting parameters Using POST:-
URL url;URLConnection urlConn;DataOutputStream printout;DataInputStream input;url = new URL (getCodeBase().toString() + "env.tcgi");urlConn = url.openConnection();urlConn.setDoInput (true);urlConn.setDoOutput (true);urlConn.setUseCaches (false);urlConn.setRequestProperty("Content-Type","application/json"); urlConn.setRequestProperty("Host", "android.schoolportal.gr");urlConn.connect(); //Create JSONObject hereJSONObject jsonParam = new JSONObject();jsonParam.put("ID", "25");jsonParam.put("description", "Real");jsonParam.put("enable", "true");
The part which you missed is in the the following... i.e., as follows..
// Send POST output.printout = new DataOutputStream(urlConn.getOutputStream ());printout.writeBytes(URLEncoder.encode(jsonParam.toString(),"UTF-8"));printout.flush ();printout.close ();
The rest of the thing you can do it.
try some thing like blow:
SString otherParametersUrServiceNeed = "Company=acompany&Lng=test&MainPeriod=test&UserID=123&CourseDate=8:10:10";String request = "http://android.schoolportal.gr/Service.svc/SaveValues";URL url = new URL(request); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true);connection.setDoInput(true);connection.setInstanceFollowRedirects(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8");connection.setRequestProperty("Content-Length", "" + Integer.toString(otherParametersUrServiceNeed.getBytes().length));connection.setUseCaches (false);DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());wr.writeBytes(otherParametersUrServiceNeed); JSONObject jsonParam = new JSONObject();jsonParam.put("ID", "25");jsonParam.put("description", "Real");jsonParam.put("enable", "true");wr.writeBytes(jsonParam.toString());wr.flush();wr.close();
References :