[SOLVED]problem with creating a URL

Put your problem here if it does not fit any of the other categories.

[SOLVED]problem with creating a URL

Postby androidonkey » Mon Feb 18, 2008 10:35 am

Hi All,

I'm having a problem creating a URL using a string variable. For example this approach returns a "Cannot be null" error due to the line URL theURL = new URL(OutboundURL):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try{
  2.  
  3.     String OutboundURL;
  4.  
  5.     String Username = "someusername";
  6.  
  7.  
  8.  
  9.     OutboundURL = "http://www.somewebsite.com/someservice.php?un=" + Username;
  10.  
  11.  
  12.  
  13.     URL theURL = new URL(OutboundURL);
  14.  
  15.     InputStream in = theURL.openStream();
  16.  
  17.     int b = in.read();
  18.  
  19.                                
  20.  
  21.     while(b != -1){                                                                            
  22.  
  23.             b = in.read();
  24.  
  25.      }
  26.  
  27. }
  28.  
  29. catch(MalformedURLException e){
  30.  
  31.  
  32.  
  33. }
  34.  
  35. catch(IOException e){
  36.  
  37.  
  38.  
  39. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


However the approach above works perfectly if I don't use a string variable:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. URL theURL = new URL("http://www.somewebsite.com/someservice.php?un=someusername");
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


My question is what's the difference between these two:
"http://www.somewebsite.com/someservice.php?un=someusername"
versus
OutboundURL = "http://www.somewebsite.com/someservice.php?un=" + Username

Thanks in advance.
Last edited by androidonkey on Tue Feb 19, 2008 8:04 am, edited 1 time in total.
androidonkey
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Feb 18, 2008 10:20 am

Top

Postby plusminus » Mon Feb 18, 2008 12:25 pm

:? There should be no difference, especially because the code probably gets optimized to the exactly the same ...

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby androidonkey » Mon Feb 18, 2008 6:39 pm

Plusminus,

I agree there shouldn't be but it seems to matter when I run it. But I found a way to make it work :)

Thanks.
androidonkey
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Feb 18, 2008 10:20 am

Postby plusminus » Mon Feb 18, 2008 7:41 pm

Hello androidonkey,

let the community know how you worked it out :)

Best Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby androidonkey » Tue Feb 19, 2008 12:32 am

Oh sorry about that. So here goes:

If you're creating a URL using more than one string variable you have to use String.valueOf() for example:

SomeURLString = "http://www.somewebsite//someservice?username=" + usernamestringvariable;

URL theURL = new URL(String.valueOf(SomeURLString));

I hope this helps - it took me a couple of hours to find out that I couldn't just plug a string variable in there like so:

URL theURL = new URL(SomeURLString);

Happy coding!
androidonkey
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Feb 18, 2008 10:20 am

Postby plusminus » Tue Feb 19, 2008 4:40 pm

Hello androidonkey,

fine to see that solution, but... does it make any sense, that it doesn't work the straight way :? :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby androidonkey » Tue Feb 19, 2008 9:33 pm

Hi plusminus,

I'm not sure why it doesn't work the straightforward way but my theory is this:

If a string variable StringA is created from other string variables:
StringA = StringB + StringC + StringD;

I think StringA doesn't actually contain the values of StringB, StringC, and StringD. What it contains are the memory addresses which point to where the values of StringB, StringC, and StringD reside. So, a URL object probably requires a string literal and not pointers to memory addresses. Using String.valueof() will move over the actual values of StringB, C, D into StringA which then turns StringA into a literal.

Let me know what you think.

Thanks,
androidonkey
androidonkey
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Feb 18, 2008 10:20 am

Postby plusminus » Tue Feb 19, 2008 9:45 pm

Hello androidonkey,

the strings get concated. Each "+" creates a new object of the concated strings.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. StringA = StringB + StringC + StringD;
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Perhaps the compiler does something wrong and when we use that function he cannot optimize :? :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby androidonkey » Tue Feb 19, 2008 9:55 pm

Hey plusminus,

You're right - I agree that it's probably the compiler, which is why it took me so long to figure out the cause of this problem. I simply wasn't doing anything wrong but i'm glad i found this alternative. Should I report this as a bug to google?

Thanks,
androidonkey
androidonkey
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Feb 18, 2008 10:20 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests