Ahmad A Bazadgha
Ahmad A Bazadgha

Reputation: 391

how can i connect my blackberry app to my website rest API?

I am trying to build my first app for blackberry mobile's but i am new @ mobile development and i am cant find the correct way to connect my app with my webiste rest API can anyone guide me where to start from?

Thanx in advance :D

Upvotes: 0

Views: 434

Answers (1)

Dhiral Pandya
Dhiral Pandya

Reputation: 10619

    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();
    }

Upvotes: 1

Related Questions