Developer

HttpConversation Workflow

Add and use HttpConversation in your Android project to support SAML-enabled registration.

Context

To successfully complete this procedure, you need to get the Android Native Client SDK libraries. The libraries are available as part of the SAP Mobile Platform Client SDK Installer. HttpConversation is shipped in AAR files

Before you can use HttpConversation, you must add associated artifacts. You also need to add dependent JARs, AARs, and configure your build settings. For the rest of the procedure, we assume that you are starting a new project for Android. If you are working with an existing project, the steps may vary.

Procedure

  1. Create a new project or open an existing one in Android Studio.
  2. Ensure that AndroidManifest.xml contains INTERNET permission:
    <uses-permission android:name="android.permission.INTERNET"/>
  3. Copy these files into the libs folder of your project:
    • HttpConversation.aar
    • HttpConvAuthFlows.aar
    • ClientLog.jar
    • sap-E2ETrace.aar
    • simple-xml.aar
    • SupportabilityFacade.aar
  4. Create a new Activity class or open an existing one.
  5. Implement the classes by adding code.
    1. Add fields and constants such as:
      //the conversation manager that will be used throughout the application
      HttpConversationManager conversationManager;
      //button to start the sample conversation
      Button startConvButton;
      //text view to present response in 
      TextView responseView;
      	
      String response;
      	
      public static final String TAG = "Sample";
      
      public static final String URL = "http://services.odata.org/V3/Northwind/Northwind.svc/";
      
    2. Prepare a conversation manager instance:
      /**
      * helper method to prepare conversationManager instance
      */
      private void prepareConversationManager(){
         //create an instance of Default configurator
         IManagerConfigurator commonConfigurator = new CommonAuthFlowsConfigurator(this);
          	
         //instantiate conversation manager
         conversationManager = new HttpConversationManager(this);
          	
         //configure the manager with the selected configurator
         commonConfigurator.configure(conversationManager);
      }
      
    3. Prepare a conversation instance:
      /**
      * helper method to prepare a simple conversation
      * @return IHttpConversation instance of conversation or null
      */
      private IHttpConversation prepareConversation(String url){
         IHttpConversation conv = null;
         try {
      	conv = conversationManager.create(new URL(url));
      	//optionally manipulate the request before it will be sent out.
      	conv.setRequestListener(new IRequestListener() {
      				
      		@Override
      		public Object onRequestHeaderSending(ISendEvent arg0) {
      			// TODO Auto-generated method stub
      			return null;
      		}
      				
      		@Override
      		public Object onRequestBodySending(ITransmitEvent arg0) throws IOException {
      			// TODO Auto-generated method stub
      			return null;
      		}
      	});
      	//implement logic that needs to be executed when the response received
      	conv.setResponseListener(new IResponseListener() {
      				
      		@Override
      		public void onResponseReceived(IReceiveEvent event) throws IOException {
      					
      					
      		   //helper method to convert an input stream to string
                   response = getStringFromInputStream(event.getStream());
      					
      		   Log.i(TAG, "got response: " + response);
      		   responseView.post(new Runnable() {
      						
      			 @Override
      			 public void run() {
      			    responseView.setText(response);
      			    }
      			 });
      					
      		}
      	});
      	
      		} catch (MalformedURLException e) {
      			e.printStackTrace();
        }
      		return conv;
      }
      
    4. Add an onCreate method such as:
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          //instatiate and initialize our conversationManager
          prepareConversationManager();
              
          //find the sample button from the current content view
          startConvButton = (Button) findViewById(R.id.button1);
              
          //find the sample button from the current content view
          responseView = (TextView) findViewById(R.id.textView1);
              
          //set onClickListener
          startConvButton.setOnClickListener(new OnClickListener() {
             @Override
      	  public void onClick(View v) {
      	     //prepare conversation
      		IHttpConversation conv = prepareConversation(URL);
      		//if the conversation was prepared successfuly start it.
      		if(conv!=null){
      		   conv.start();
      		}
      	  }
         });
      }