×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    REST in a J2ME/Java ME application

    Hi everyone,

    What would be the best way to consume a REST service in a J2ME/Java ME application? Data is in JSON format. Unfortunately there don't seem to be any helper libraries available.

    Many thanks

  2. #2
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: REST in a J2ME/Java ME application

    Hello Infms !

    I am using https://github.com/upictec/org.json.me/, with success, for a REST/JSON service.
    The usage is straightforward, just download the buffer and decode it using json parser.


    Code:
        // downloading ....
        public boolean Response20X(HttpConnection connection, Hashtable cookies) {
            
            boolean result = false;
            StringBuffer strf = null;
            response = null;
            
            try {
                InputStream is = connection.openInputStream();
                InputStreamReader isr = new InputStreamReader(is, "UTF-8");
    
                int ch;
                strf = new StringBuffer();
                while ((ch = isr.read()) != -1) {
                    strf.append((char) ch);
                }
                
                response = strf.toString();
                strf = null;
                // TODO do not decode before checking reponse code
                result = data.fromJSONString(response);
                response = null;
                isr.close();
                is.close();
                
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
            return result;
        }
    Code:
            // decoding ....
    	public boolean fromJSONString(String str) {
            boolean result = false;
            
            try {
                Vector _data = new Vector();
                JSONObject jobj = new JSONObject(str);
                JSONArray feeds = jobj.getJSONArray("data");
                
                if(feeds != null) {
                    for(int n = 0 ; n < feeds.length(); n++) {
                        // more decoding ...
                    }
                }
                result = true;
                
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
            
            return result;
        }
    Marcelo Barros

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Re: REST in a J2ME/Java ME application

    Hi Marcelo,

    Thanks for the reply and example code. Say for example I had a REST service I wanted to access at htt://alpha.myrestservice.com/ and send the request: GET api/lists

    How would I go about using the library to send such a request?

    Thanks

  4. #4
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: REST in a J2ME/Java ME application

    The request should be done via some HttpConnection call. For instance:

    Code:
    String url = "https://api.instagram.com/v1/users/self/feed";
    
    HttpConnection connection = (HttpConnection) Connector.open(url,Connector.READ_WRITE,true);
    connection.setRequestMethod(HttpConnection.GET);
    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Series40)");
    connection.setRequestProperty("Connection", "close");          
    int code = connection.getResponseCode();
    
    if(code == HttpConnection.HTTP_OK) {
    	// handle result, read data ...
    } else {
    /// ...            
    }
    connection.close();
    Check the demo "PicasaViewer" for another example: http://is.gd/JmLsV8

    See you
    Marcelo Barros

  5. #5
    Super Contributor
    Join Date
    Nov 2007
    Location
    Sertaozinho/Brazil
    Posts
    768

    Re: REST in a J2ME/Java ME application

    Moreover, have you tried Tantalum ? It seems pretty good.

    https://projects.developer.nokia.com/Tantalum

    "Simplified XML and JSON parsing into value objects"

    We will give it a try ASAP.

    Marcelo Barros

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Re: REST in a J2ME/Java ME application

    I ended up using the Tantalum library (for handling JSON) along with the HttpConnection GET request, similar to what you posted below. Everything works great! Thanks again

Similar Threads

  1. Java.io.IOException in J2me application
    By DileepKumar in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2013-03-22, 08:13
  2. Consume WCF REST service in J2ME on Nokia E72
    By nokiahemant in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2011-12-08, 13:43
  3. Call an EXE application from Java (J2ME)
    By rcaboni in forum Symbian
    Replies: 4
    Last Post: 2009-08-11, 13:57
  4. Launch J2ME application on a emulator from a java application
    By bourne_jason in forum Mobile Java General
    Replies: 1
    Last Post: 2008-10-17, 11:47
  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
  •  
×