Blackberry Send&通过Post方法接收数据

时间:2012-12-14 07:05:05

标签: json post blackberry httpconnection

通过POST方法在服务器上发送数据。 并收到JSON响应。 黑莓 - 回答。 快乐编码

1 个答案:

答案 0 :(得分: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);