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

Web and WebWorks Development

Reply
New Contributor
Posts: 6
Registered: ‎08-21-2010
My Device: 8900

Calling Web Services chain to get to the result page

Hi All,

I'm developing an app in which if user clicks 'Login' button my app should call 2 web services to show the next page. Here the 1st web service output becomes the 2nd web service input. I'm able to call only one web service and getting expected result. If I call both first web service is getting successful but the second one is giving 401 error. Where am I going wrong..? Below is my code snippet: Please help me ASAP.

 

Login Function:

 

private void login() throws JSONException
        {
            createSharedDevice();
            getOperatorList();
        }

 

private void createSharedDevice() throws JSONException
{
               // Call WEB SERVICE 1 here
            
               webURL = baseURL + queryString;
          ConnectionThread connThread1 = new ConnectionThread();
         connThread1.webServiceCall("POST", webURL,  "CreateSharedDevice", "TEST100001", "test1234");                             
          

       connThread1.start();
           
           while (connThread1.sending)
           {
                      Status.show("Please wait...");
           }
              
           if (connThread1.sendResult)
           {   
                String webService1Response = connThread1.responseContent;
                //connThread1.stop();
               
                if(webService1Response != null && webService1Response.length() > 0)
                {
                   JSONObject outer=new JSONObject(webService1Response);                   
                    JSONObject inner = outer.getJSONObject("UserProfile");                   
                   
                    sSharedDeviceUserID = (String)inner.get("sharedDeviceUserId");
                    sSharedDevicePassword =(String)inner.get("sharedDeviceUserPassword");                    
                                        
                    EventLogger.logEvent(EventLoggerID,(new String("SharedDeviceUserID = ")+sSharedDeviceUserID).getBytes(),EventLogger.DEBUG_INFO);
                    EventLogger.logEvent(EventLoggerID,(new String("SharedDevicePassword = ")+sSharedDevicePassword).getBytes(),EventLogger.DEBUG_INFO);                    
                }
           }
              
             else
            {
                Status.show("Request Failed");
                onClose();
            }
        }

 

 

private void getOperatorList() throws JSONException
 {
          if(sSharedDeviceUserID != null && sSharedDeviceUserID.length() > 0 &&
                    sSharedDevicePassword != null && sSharedDevicePassword.length() > 0 )
                 {
                     webURL = baseURL + queryStr;

                  ConnectionThread connThread2 = new ConnectionThread();
                  connThread2.webServiceCall("GET", webURL, "GetOperatorList", sSharedDeviceUserID, sSharedDevicePassword);            
                 connThread2.start();
                  
                   while (connThread2.sending)
                   {
                      Status.show("Please hold...");
                   }
                     
                       if(connThread2.sendResult)
                     {
                        String webService2Response = connThread2.responseContent;
                      //connThread2.stop();
                 
                       if(webService2Response != null && webService2Response.length() > 0)
                    {
                             EventLogger.logEvent(EventLoggerID,(new String("Web Service 2: ") + connThread2.responseContent ).getBytes(),EventLogger.DEBUG_INFO);                           
                       }
                     }
                      else
                {
                    Status.show("Request Failed");
                    onClose();
                }
             }
        }

 

--------------------------------------------------------------

And below is my ConnectionThead.java class

--------------------------------------------------------------

The key method is highlighted below..

 

public class ConnectionThread extends Thread {

   private long EventLoggerID = 0x12345678L;
    private boolean start = false;

    private boolean stop = false;

    private String url;

    private String data;
   
    private String loginCred;
   
    private String method;

    public boolean sendResult = false;

    public boolean sending = false;
   
    private String requestMode = HttpConnection.POST;
   
    public String responseContent;

    public void run() {
        EventLogger.logEvent(EventLoggerID,(new String("In RUN")).getBytes(),EventLogger.DEBUG_INFO);
        while (true) {
            if (start == false && stop == false) {
                try {
                    sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (stop) {
                return;
            } else if (start) {
                try {
                                       http();
                                } catch (IOException e) {
                                      // TODO Auto-generated catch block
                                     e.printStackTrace();
                           }
            }
        }
    }
   
    private void getResponseContent( HttpConnection conn ) throws IOException {
        InputStream is = null;
        is = conn.openInputStream();

            // Get the length and process the data
            int len = (int) conn.getLength();
            if ( len > 0 ) {
                int actual = 0;
                int bytesread = 0;
                byte[] data = new byte[len];
                while ( ( bytesread != len ) && ( actual != -1 ) ) {
                    actual = is.read( data, bytesread, len - bytesread );
                    bytesread += actual;
                }
                responseContent = new String (data);
            }
            else
            {
                int ch;
                while ( ( ch = is.read() ) != -1 ) {

                }
            }
           
         
 
    }
   
    private void http() throws IOException 
    {       
        EventLogger.logEvent(EventLoggerID,(new String("In HTTP URL = ")+url).getBytes(),EventLogger.DEBUG_INFO);
        HttpConnection conn = null;
        HttpConnection conn2 = null;
        OutputStream out = null;
        OutputStream out2 = null;
        int responseCode = 0;

        try
        {                                  
               byte[] encoded = Base64OutputStream.encode(loginCred.getBytes(), 0, loginCred.length(), false, false);         
                                
                conn = (HttpConnection) Connector.open(url);
                
            
              if("CreateSharedDevice".equals(method))
              {
                conn.setRequestMethod(requestMode);           
                conn.setRequestProperty("Accept", "application/json");
                conn.setRequestProperty("Authorization", "Basic " + new String(encoded));
              }
              else if("GetOperatorList".equals(method))
              {
                
                conn.setRequestMethod(requestMode);           
                conn.setRequestProperty("Content-Type", "text/html");
                conn.setRequestProperty("Accept", "application/json");
                conn.setRequestProperty("Authorization", "Basic " + new String(encoded));

              }

 

                                
                 out = conn.openOutputStream();
                 out.write(data.getBytes());
                 out.flush();
                 responseCode = conn.getResponseCode();

            
            
            if (responseCode != HttpConnection.HTTP_OK)
            {
                EventLogger.logEvent(EventLoggerID,(new String("Not OK")).getBytes(),EventLogger.DEBUG_INFO);
                sendResult = false;
                responseContent = null;
            }
            else
            {
               EventLogger.logEvent(EventLoggerID,(new String("OK")).getBytes(),EventLogger.DEBUG_INFO);
               sendResult = true;
               getResponseContent( conn );
             }
            start = false;
            sending = false;
        }
       
        catch (Exception e)
        {
            EventLogger.logEvent(EventLoggerID,(new String("Error: ")+e).getBytes(),EventLogger.DEBUG_INFO);
            start = false;
            sendResult = false;
            sending = false;
        }
       
        finally
        {          
            if(conn != null)
                conn.close();
         }
    }

   

public void webServiceCall(String reqMode, String url, String method, String userID, String password)
    {
        this.url = url;
        this.data = "";
       
        if("POST".equals(reqMode))
           requestMode = HttpConnection.POST;
        else if("GET".equals(reqMode))
              requestMode = HttpConnection.GET;
       
        this.method = method;
        this.loginCred = userID+":"+password;
       
        sendResult = false;
        sending = true;
        start = true;
    }

    public void stop()
    {         
       stop = true;
    }

}


 

 

 

 

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Calling Web Services chain to get to the result page

I think your question would have a better chance getting answered in the Java forum.  This forum is mainly for HTML, CSS and JavaScript questions for building web pages or BlackBerry Widgets

 

Would you like me to move your message to the Java forums? 

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter