Simple HTTP Post Request - application/json
-
Hello there. I am trying to make a simple http POST request using HttpUrlConnection. Problem is that response code is 200 but the response message NOT success. You can see that I set all the necessary header parameters. I am supposed to get
success
message. But I get{"status":false,"message":"Invalid access"}
with responseCode = 200. Below is the code (I am developing against Lollipop)protected SubmitRequestTask.ResponseObject doInBackground(Object... object)
{
String encodedStr = URLEncoder.encode(object.getDataToSendRAW(), "UTF-8");BufferedReader reader = null; int responseCode = -1; try { URL url = new URL(/\*My SERVER URL\*/); //Converting address String to URL Map headerParams = new HashMap(); headerParams.put("Accept", "application/json"); headerParams.put("Content-Type", "application/json"); HttpUrlConnection networkConnection = MakeRequest( url, "POST", headerParams, null); if(networkConnection == null) return new ResponseObject(-1, "Uknown Error"); StringBuilder sb = new StringBuilder(); reader = new BufferedReader(new InputStreamReader(networkConnection.getInputStream())); while ((response = reader.readLine()) != null) sb.append(response + "\\n"); response = sb.toString(); } catch(Exception ex) {} finally { responseCode = networkConnection.getResponseCode(); networkConnection.disconnect(); }
return new ResponseObject(responseCode, response);
}private HttpUrlConnection MakeRequest(URL url, String method, Map headerParams, NetworkConfig config)
{
HttpURLConnection htp_url_connection = (HttpURLConnection) url.openConnection();
htp_url_connection.setRequestMethod(method);if (headerParams != null && method.trim().toLowerCase().equals("post")) { htp\_url\_connection.setDoOutput(true); for (Map.Entry entry : headerParams.entrySet()) { (htp\_url\_connection.setRequestProperty(entry.getKey(), entry.getValue()); } if (encodedStr.trim().length() > 0) { OutputStreamWriter writer = new OutputStreamWriter(htp\_url\_connection.getOutputStre
-
Hello there. I am trying to make a simple http POST request using HttpUrlConnection. Problem is that response code is 200 but the response message NOT success. You can see that I set all the necessary header parameters. I am supposed to get
success
message. But I get{"status":false,"message":"Invalid access"}
with responseCode = 200. Below is the code (I am developing against Lollipop)protected SubmitRequestTask.ResponseObject doInBackground(Object... object)
{
String encodedStr = URLEncoder.encode(object.getDataToSendRAW(), "UTF-8");BufferedReader reader = null; int responseCode = -1; try { URL url = new URL(/\*My SERVER URL\*/); //Converting address String to URL Map headerParams = new HashMap(); headerParams.put("Accept", "application/json"); headerParams.put("Content-Type", "application/json"); HttpUrlConnection networkConnection = MakeRequest( url, "POST", headerParams, null); if(networkConnection == null) return new ResponseObject(-1, "Uknown Error"); StringBuilder sb = new StringBuilder(); reader = new BufferedReader(new InputStreamReader(networkConnection.getInputStream())); while ((response = reader.readLine()) != null) sb.append(response + "\\n"); response = sb.toString(); } catch(Exception ex) {} finally { responseCode = networkConnection.getResponseCode(); networkConnection.disconnect(); }
return new ResponseObject(responseCode, response);
}private HttpUrlConnection MakeRequest(URL url, String method, Map headerParams, NetworkConfig config)
{
HttpURLConnection htp_url_connection = (HttpURLConnection) url.openConnection();
htp_url_connection.setRequestMethod(method);if (headerParams != null && method.trim().toLowerCase().equals("post")) { htp\_url\_connection.setDoOutput(true); for (Map.Entry entry : headerParams.entrySet()) { (htp\_url\_connection.setRequestProperty(entry.getKey(), entry.getValue()); } if (encodedStr.trim().length() > 0) { OutputStreamWriter writer = new OutputStreamWriter(htp\_url\_connection.getOutputStre
That is correct; the HTTP response is 200 telling you that the server responded with some data. The response data is a JSON reply which contains information about the JSON request. You need to contact the owners of the website, or the documentation, to find out why it returned the "Invalid access" status.