Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 139
Registered: ‎02-23-2012
My Device: Blackberry bold
My Carrier: Vodafone India

Https Post Web Service with Json post data

Hi All,

 

I am facing a problem when I tried to use a login webservice (i.e; https post) that require to add header and json to post data.

 

I am getting response code as 401.

 

below is my code

 

ConnectionFactory conFactory = new ConnectionFactory();
            conFactory.setTimeLimit(1000);

HttpsConnection httpConnection2 =  (HttpsConnection) conFactory.getConnection(urlStr).getConnection();
            httpConnection2.setRequestMethod(HttpsConnection.POST);
            httpConnection2.setRequestProperty("Authorization","Basic "+sig);
            httpConnection2.setRequestProperty("Accept", "application/json");
            httpConnection2.setRequestProperty("Content-Type", "application/json");
            //httpConnection2.setRequestProperty("Content-Length", String.valueOf(jsonStr.getBytes().length));
            OutputStream out = httpConnection2.openOutputStream();
            out.write(jsonStr.getBytes());
            final int Response = httpConnection2.getResponseCode();
            is=httpConnection2.openInputStream();
            _isr = new InputStreamReader(is);
            System.out.println(" RESPONSE CODE : "+Response);
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                        Dialog.alert("Response code is : " +Response);
                }
            });
            if(httpConnection2.getResponseCode() == HttpsConnection.HTTP_OK ) //If connection is ok then read the response
             {
                      System.out.println("Http result-----------"+new String(IOUtilities.streamToBytes(is)));
             }  
            out.flush();
            out.close();
        }catch (Exception e)
            {
            e.printStackTrace();
            System.out.println("Exception is  ................. " +e.getMessage());
            }
            finally
            {            
                  try
                  {
                    if(_isr!=null)
                        _isr.close();
                    if(is!=null)
                        is.close();
                  } catch (IOException e)
                      {                  
                      e.printStackTrace();
                      }     
            }

 

Please help me to get out of this.

 

Thank you in advance.

warm regards
Mohammed Aziz
Retired
Posts: 83
Registered: ‎10-10-2012
My Device: Z30
My Carrier: Bell

Re: Https Post Web Service with Json post data

Hi Mohammed,

Is this still an issue for you?

If the problem was resolved - perhaps post the answer for others? (just a suggestion).

g