Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Consume WCF REST service in J2ME on Nokia E72

    Hello!

    I want to know how to consume a WCF REST web service in J2ME (OS Symbian S60) on Nokia E72.
    My web service details: Web service : WCF + REST
    Hosting: IIS
    Binding: webHttpBinding

    Please tell me client code ?


    Thanks in advance

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Consume WCF REST service in J2ME on Nokia E72

    Hi nokiahemant and welcome to Nokia's Discussion Boards,

    A few things that you normally consider when implementing a client for a REST web service are the following:

    1. The retrieval method, either GET or POST
    2. The service's URL.
    3. The content type parameters, including the MIME type (e.g. "text/xml") or the encoding (e.g. "utf-8")
    4. Whether a secure http connection is needed or not.

    an example for connecting to such a web service using POST and MIME "text/xml" is the following:
    Code:
    private String connect(String targetUrl) 
    	{
    		String response="";
    		HttpConnection connection = null;
    		InputStream inputstream = null;
    	    try
    	    {
    	    	connection = (HttpConnection) Connector.open(targetUrl);
    	        //HTTP Request
    	        connection.setRequestMethod(HttpConnection.POST);
    	        connection.setRequestProperty("Content-Type","text/xml");
    	        connection.setRequestProperty("Connection", "close");
    	        // HTTP Response
    	        if (connection.getResponseCode() == HttpConnection.HTTP_OK)
    	        {	          
    	        	inputstream = connection.openInputStream();
    	        	int length = (int) connection.getLength();
    	        	if (length != -1)
    	        	{
    	        		byte incomingData[] = new byte[length];
    	        		inputstream.read(incomingData);
    	        		response = new String(incomingData);
    	        	}
    	        	else  
    	        	{
    	        		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    	        		int ch;
    	        		while ((ch = inputstream.read()) != -1)
    	        		{
    	        			bytestream.write(ch);
    	        		}
    	        		response = new String(bytestream.toByteArray());
    	        		bytestream.close();	            
    	        	}	          
    	        }
    	    }	
    	    catch(IOException error)
    	    {
    	       f.append("Caught IOException: " + error.toString());	       
    	    }
    	    finally
    	    {
    	    	if (inputstream!= null)
    	        {
    	          try 
    	          { 
    	            inputstream.close();
    	          }
    	          catch( Exception error)
    	          {
    	             /*log error*/
    	          }
    	        }
    	        if (connection != null)
    	        {
    	          try
    	          {
    	             connection.close();
    	          }
    	          catch( Exception error)
    	          {
    	             /*log error*/
    	          }
    	        }
    	    }
    	    return response;		
    	}
    On top of that, you might need to implement xml parsing, in order to extract the information you receive from the server.

    XML parsing example using JSR-172 on the Wiki:
    http://www.developer.nokia.com/Commu..._using_JSR_172
    and Java Docs for JSR-172:
    http://library.developer.nokia.com/nav/0_7_7

    You might also need additional information and code snippets on http connections in Java MIDlets:
    http://library.developer.nokia.com/t...onnection.html

    Note, that if you need secure http connection, your application should be signed.

Similar Threads

  1. How do i consume web service ?
    By busilo2002 in forum Mobile Java Networking & Messaging & Security
    Replies: 10
    Last Post: 2013-10-25, 10:40
  2. consume the webservice using the J2ME
    By kpvrlakshmi in forum Mobile Java General
    Replies: 3
    Last Post: 2011-01-07, 12:29
  3. Consume web service
    By amit.mangal in forum Symbian
    Replies: 7
    Last Post: 2011-01-04, 11:30
  4. hey nokia please return to capture://video for the rest of the S40 devices
    By sbwoodside in forum Mobile Java Media (Graphics & Sounds)
    Replies: 7
    Last Post: 2007-11-19, 03:40
  5. REST technology for my j2me application
    By smart@gayu in forum Mobile Java General
    Replies: 1
    Last Post: 2007-04-23, 11:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×