Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Simple HTTP Post Request - application/json

Simple HTTP Post Request - application/json

Scheduled Pinned Locked Moved Android
helpsysadminjson
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    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
    
    L 1 Reply Last reply
    0
    • D Django_Untaken

      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups