Search code examples
jsonpostblackberryhttpconnection

Send JSONObject using HttpConnection POST with BlackBerry


I'm trying to do a blackbery application that can send a JSONObject through a HttpConnection POST request. The JSONObject is

{ 
   "Contrasena" : "hy1tSPQc3K4IlSZLvd7U7g==", 
   "Plataforma" : "A", 
   "Usuario" : "user2323" 
}

Anyone know how?


Solution

  • You can use URLEncodedPostData class for this. Use like

      httpConn.setRequestMethod(HttpConnection.POST);
    
      httpConn.setRequestProperty("Content-Type", "application/json");
    
      URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
    
      encPostData.setData(JsonString);//set your jsonstring
    
      byte[] postData = encPostData.toString().getBytes("UTF-8");   
    
      httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length)); 
    
      httpConn.openOutputStream().write(encPostData.getBytes()); 
    
      int Response = httpConn.getResponseCode();