如何将我的黑莓应用程序连接到我的网站rest API?

时间:2011-09-28 10:01:19

标签: rest blackberry mobile blackberry-eclipse-plugin

我正在尝试构建我的第一个黑莓手机应用程序,但我是新的@移动开发,我无法找到正确的方式将我的应用程序与我的网站休息API连接,任何人都可以指导我从哪里开始?

提前完成:D

1 个答案:

答案 0 :(得分:1)

    HttpConnection conn = null;
    InputStream is = null;
    ByteArrayOutputStream bos = null;
    String response = null;
    String connectionURL = null;



    try {

        connectionURL = "THIS CONTAIN YOUR API URL"

        conn = (HttpConnection) Connector.open(connectionURL);
        conn.setRequestProperty("Content-Type","application/json");
        System.out.println("Response code : "+conn.getResponseCode());

        if(conn.getResponseCode() == HttpConnection.HTTP_OK)
        {

            is = conn.openInputStream();
            int ch=-1;
            bos = new ByteArrayOutputStream();
            while((ch = is.read())!=-1)
            {
                bos.write(ch);
            }
            response = new String(bos.toByteArray());
            System.out.println("Response : "+response);
        }
    } catch (Exception e) 
    {
        System.out.println("Exception .."+e);

    }finally 
    {

            if(conn != null)
                conn.close();
            if(is != null)
                is.close();
            if(bos != null)
                bos.close();
    }