-2

Sending Data on server via POST Method. And receiving the JSON Response. Blackberry - Answered. Happy Coding

rekire
  • 47,260
  • 30
  • 167
  • 264
Ahmad Shahwaiz
  • 1,432
  • 1
  • 17
  • 35

1 Answers1

0
StringBuffer postData = new StringBuffer();

            httpConn = (HttpConnection) Connector.open(URL);
            httpConn.setRequestMethod(HttpConnection.POST);

            postData.append("?username="+username);
            postData.append("&password="+pass);
            postData.append("&projectcode="+projectid);
            String encodedData = postData.toString();

            httpConn.setRequestProperty("Content-Language", "en-US");
            httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            httpConn.setRequestProperty("Content-Length",(new Integer(encodedData.length())).toString());
            byte[] postDataByte = postData.toString().getBytes("UTF-8");

            OutputStream out = httpConn.openOutputStream(); 
            out.write(postDataByte);
            out.close();

            httpConn.getResponseCode();

            is = httpConn.openInputStream(); 

            StringBuffer buffer = new StringBuffer();
            int ch = 0;

            while (ch != -1) {
                ch = is.read();
                buffer.append((char) ch);
            }

            String json = buffer.toString();

            Dialog.alert("Received Json: "+json);
Ahmad Shahwaiz
  • 1,432
  • 1
  • 17
  • 35
  • There are a few things I don't like, but the worst is that in order for this code to issue the Dialog.alert(..) that it does at the end, it must be running on the Event Thread, and of course it should not do that because it is blocking with network activity. This is not production code. – Peter Strange Oct 04 '13 at 00:48
  • @PeterStrange It was just for debugging purpose to show the output on screen. and yes this is not production code i agree – Ahmad Shahwaiz Oct 04 '13 at 09:03