×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Hello

    I'm having trouble opening URLs that contain certain query strings using (HttpConnection)Connector.open(url). The problem only occurs in the S60 SDK emulator and when I test it on my mobile device Nokia N86 8MP, so it appears to be an issue specific to Symbian. The code works on all other emulators provided with the Java ME SDK in NetBeans, and if I simply open the URLs in any desktop browser.

    The problem occured during development of a Java ME MIDlet that opens local URLs to retrieve and process information in the HTML response. The code I will provide is simplified examples to reflect the same problem, and with URLs accessible from the internet so you can see for yourselves.
    The query string in the URL don't do anything in this example. They have a meaning in my actual application, but are only there now to show the problem.

    Function GetHttpResponse to get a HTML response from a HTTP connection:
    Code:
    package httptest;
    
    import java.io.*;
    import javax.microedition.io.*;
    
    public class HttpTest
    {
        public static String GetHttpResponse(String url) throws IllegalArgumentException, IOException
        {
            String htmlstring = null;
            StringBuffer sbuffer = new StringBuffer();
            InputStream is = null;
            HttpConnection httpconn = null;
    
            long len = 0;
            int ch = 0;
    
            httpconn = (HttpConnection)Connector.open(url);
    
            is = httpconn.openInputStream();
    
            len = httpconn.getLength();
            if (len != -1) {
                for (int i = 0; i < len; i++) {
                    if ((ch = is.read()) != -1) {
                        sbuffer.append((char) ch);
                    }
                }
            } else {
                while ((ch = is.read()) != -1) {
                    len = is.available();
                    sbuffer.append((char) ch);
                }
            }
            htmlstring = sbuffer.toString();
            is.close();
            httpconn.close();
    
            return htmlstring;
        }
    }
    Running code that works (cut from a MIDlet):
    Code:
    String url = "http://home.broadpark.no/~whomme/test/testhtml.html" + 
                 "?foo=GetMediaLocation(music;D:\\pub\\music\\1_my_lossless\\)";
    
    try
    {
        String response = httptest.HttpTest.GetHttpResponse(url);
        getResponseStringItem().setLabel("Response OK!");
        getResponseStringItem().setText(response);
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
        getResponseStringItem().setLabel(ex.toString());
        getResponseStringItem().setText(ex.getMessage());
    }
    Now, if I set the query string in the URL to something slightly different, the (HttpConnection)Connector.open(url) method will cast a java.lang.IllegalArgumentException.

    This doesn't work:
    Code:
    String url = "http://home.broadpark.no/~whomme/test/testhtml.html" + 
                 "?foo=GetMediaLocation(music;D:\\pub\\music\\3_other_unsorted\\)";
    Note that the only difference in the two URLs is part of the string (representing a path) in the query (after "?" in URL).

    Stack trace:
    java.lang.IllegalArgumentException
    at com.symbian.j2me.net.URI.ensureChar(URI.java:616)
    at com.symbian.j2me.net.URI.ensureStarUric(URI.java:597)
    at com.symbian.j2me.net.URI.parse(URI.java:286)
    at com.symbian.j2me.net.URI.<init>(URI.java:144)
    at com.symbian.j2me.midp.runtimeV2.Adapter.ensureConnectionPermission(Adapter.java:51)
    at com.symbian.midp.runtime.Security.ensureConnectionPermission(Security.java:23)
    at com.symbian.gcf.ProtocolBase.ensurePermission(ProtocolBase.java:95)
    at com.symbian.gcf.ProtocolBase.openConnection(ProtocolBase.java:69)
    at com.symbian.midp.io.protocol.http.Connection.createConnection(Connection.java:20)
    at com.nokia.mj.impl.vmport.J9GcfConnectionBase.setParameters2(J9GcfConnectionBase.java:43)
    at javax.microedition.io.Connector.open(Unknown Source)
    at javax.microedition.io.Connector.open(Unknown Source)
    at httptest.HttpTest.GetHttpResponse(Unknown Source)
    at hello.TestMIDlet.ListOfUrlsAction(Unknown Source)
    at hello.TestMIDlet.commandAction(Unknown Source)
    at javax.microedition.lcdui.Displayable.callCommandAction(Displayable.java:164)
    at javax.microedition.lcdui.List.handleEvent(List.java:351)
    at javax.microedition.lcdui.Toolkit.handleDisplayableEvent(Toolkit.java:536)
    at com.symbian.lcdjava.lang.SystemExtensions._dispatchCallbacks(Native Method)
    at com.symbian.lcdjava.lang.SystemExtensions.dispatchCallbacks(SystemExtensions.java:35)
    at com.symbian.midp.runtime.KVMEventProcessor.run(KVMEventProcessor.java:23)

    According to javadocs, IllegalArgumentException is thrown from Connector.open "if a parameter is invalid".
    In my actual application I take care of stuff like URL encoding, but there wasn't need for that in these URLs, so i left it out. Besides, there aren't any special characters in one of the URLs that doesn't appear in the other, and only one of them causes an exception.

    Does anyone have an explaination to why one of the URLs works fine, while the other one doesn't?
    Source for this example can be downloaded here.

    I'm using:
    NetBeans IDE 6.9 Beta
    S60 3rd Edition SDK for Symbian OS, Feature Pack 2 v1.1

    Thanks
    Last edited by pressplayontape; 2010-06-01 at 10:26.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    I'm not sure why it works with one but not the other, but you must encode any characters that are not part of:

    Code:
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
    a b c d e f g h i j k l m n o p q r s t u v w x y z 
    0 1 2 3 4 5 6 7 8 9 - _ . ~
    except for reserved characters like "=" and "&", when they are used for their special meaning.

    Graham.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Hi pressplayontape,


    Follow this post it might be helpful to you

    http://discussion.forum.nokia.com/fo...d.php?t=181351

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    3

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Thank you Graham, you pinpointed the problem.
    As I mentioned I encode the URLs in my application, but the method I used was incomplete. You made me go over it again and I found that it didn't take care of backslashes.

    Previous method:
    Code:
    public static String PercentEncode(String urlstring)
    {
        StringBuffer encoded = new StringBuffer();
        for(int i=0; i<urlstring.length(); i++)
        {
            char ch=urlstring.charAt(i);
            switch(ch)
            {
                case '<': encoded.append("%3C"); break;
                case '>': encoded.append("%3E"); break;
                case '/': encoded.append("%2F"); break;
                case ' ': encoded.append("%20"); break;
                case ':': encoded.append("%3A"); break;
                case '-': encoded.append("%2D"); break;
                default: encoded.append(ch); break;
            }
        }
        return encoded.toString();
    }
    Added this line to make it work:
    Code:
                case '\\': encoded.append("%5C"); break;
    bhanuchandar.k's post also seems useful. Haven't tried it yet, but it looks like a good way to generalize the method to handle all characters.

    It still puzzles me however, that one of the URLs worked and the other didn't. It is basically the reason I told myself that encoding wasn't the issue here when I troubleshooted the problem... After all, the first URL didn't cause an error, totally unencoded. Still curious, but I'm just happy it works now :-)

    Thanks

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Hi pressplayontape,

    It is better to do the encoding In this way

    StringBuffer sb = new StringBuffer("");
    char c;
    for (int i = 0; i < s.length(); i++) {
    c = s.charAt(i);
    if ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
    sb.append(c);
    continue;
    }

    if (c > 15) {
    sb.append("%" + Integer.toHexString( (int) c));
    }else {
    sb.append("%0" + Integer.toHexString( (int) c)) ;
    }
    }
    this you need to do for the vale part of the query string .

    Ex : param=encode(value) Like this .

    This will prevent in feature if you get any new characters as well rather than handling the one specific character

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    3

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Thanks, good method. I have it noted already :-)
    Quote Originally Posted by pressplayontape View Post
    bhanuchandar.k's post also seems useful. Haven't tried it yet, but it looks like a good way to generalize the method to handle all characters.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Hello,

    The program threw the IllegalArgumentException when the connector open the url encoded.

    //code:
    String url = "http://test.demo.com:8080/hello/location/111.11,37.00/peoples;distance=600;page=0;page_size=10?_type=json&securityToken=767e02f5152de84b5e6ab2864c2"

    HttpConnection conn = (HttpConnection) Connector.open(url);

    java.lang.IllegalArgumentException: Cannot parse scheme: http%3a%2f%2fmytest%2demo%2ecom%3a8081%2fhello%2flocation%2f111%2e00%2c37%2e0
    at javax.microedition.io.Connector.getScheme(Unknown Source)
    at javax.microedition.io.Connector.open(Unknown Source)



    Thanks.
    Last edited by xinyun197; 2010-06-07 at 12:55.

  8. #8
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Quote Originally Posted by xinyun197 View Post
    //code:
    String url = "http://test.demo.com:8080/hello/location/111.11,37.00/peoples;distance=600;page=0;page_size=10?_type=json&securityToken=767e02f5152de84b5e6ab2864c2"
    Thanks.
    URL string can not have blanks or , replace it with . it will work and remove peoples;distance=600;page=0

    you have issues with your URL, check with the server team.

    It may be "http://test.demo.com:8080/hello/location/111.11.37.00/peoples?_type=json&securityToken=767e02f5152de84b5e6ab2864c2"
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  9. #9
    Regular Contributor
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    52

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Hi I am having problem with this url and I am getting java.lang.IllegalArgumentException


    https://s3-ap-southeast-1.kkie.com/p...downloadedXML/
    news/news.xml

    Can somebody point out why I am not getting any response??
    Thanks
    rj12info

  10. #10
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: Some URLs cause IllegalArgumentException with (HttpConnection)Connector.open

    Hi rj12info ,

    Are you able to get the response if you execute it on desktop browser ? llegalArgumentException Exception comes if you have any special characters in your url. I have executed this url I got request timeout .

Similar Threads

  1. Replies: 14
    Last Post: 2010-10-16, 08:48
  2. Opening multiple url's to web browser
    By KeijoKoodaaja in forum [Archived] Flash Lite on Nokia Devices
    Replies: 6
    Last Post: 2009-12-02, 07:21
  3. To read Urls in the Auto. Bookmark folder
    By manjusg in forum Symbian
    Replies: 3
    Last Post: 2009-09-08, 14:23
  4. How to send multiple XML files to different URL's at the same time.
    By ash_21 in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2009-06-16, 08:19
  5. Reading URLs
    By tboufflet in forum Near Field Communication
    Replies: 1
    Last Post: 2007-08-24, 08:13

Posting Permissions

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