Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Get string from url

Good morning!

 

I'm trying get a string from url on server, but the application stop in StreamConnection. The code is:

 

public static String getPage(String url) {
String response = "";

try {
System.out.println("###########\nURL a transferir:\n"+url);
StreamConnection s = (StreamConnection)Connector.open(url,Connector.READ_WRITE,true);

System.out.println("###########\nStream Connection:");
InputStream input = s.openInputStream();

System.out.println("###########\nInput Stream:");
byte[] data = new byte[256];
int len = 0;
StringBuffer raw = new StringBuffer();

while( -1 != (len = input.read(data))) {
raw.append(new String(data, 0, len));
}
System.out.println(raw+"this is raw data");

response = raw.toString();

input.close();
s.close();
} catch(Exception e) {
System.out.println(e);

}

return response;
}

 

Cumps Diogo Pinto

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get string from url

Have you looked at htmlDemo.  I think it does exactly this and it demonstrates how to execute networking off the Event Thread.

 

In addition, I recommend that you put network in the Search box and review any links you think might be useful.  I would certainly review this one:

http://supportforums.blackberry.com/t5/Java-Development/Networking-Transports-II/ta-p/446742

Please use plain text.
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Re: Get string from url

I've been experimenting with HTTPDemo but it happens to me the same thing the following message "java.io.InterruptedIOException: Local connection timed out after ~ 120000"

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get string from url

It woud have been useful to know that right from the start.  Please give us as much informaiton as you can when you ask a quesiton. 

 

The timeout means you can't connect to get the information.   You need to understand networking better.  Start here:

 

http://supportforums.blackberry.com/t5/Java-Development/Networking-Transports-II/ta-p/446742

 

Review this:

 

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...

 

and then look at the documentation for ConnectionFactory.

Please use plain text.
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Re: Get string from url

Well I was a bit the same :smileyfrustrated:.

In Iphone and android I did it without much problem and very easy!

What I want is just to get information php a file on the server, as simple as that!

 

It can not be that complicated to say but I do not need anything. Have a suggestion to make it so?

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get string from url

I've answered a very similar quesiton here:

http://supportforums.blackberry.com/t5/Java-Development/To-make-an-HTTP-call-in-BB-7-1/td-p/2101977

 

Please check that out and see if it helps. 

Please use plain text.
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Re: Get string from url

I've been watching this post and in my case I do not get an xml file but the result giving the url and get it to a string. The url is constructed so "http://linkserver/appname/file.php?param=1" the server returns a text that I want to fetch and analyze later.

The way that this post has not got anything to get it :smileyfrustrated:

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get string from url

[ Edited ]

OK, I am not clear that you have understood what I was saying in this post:

http://supportforums.blackberry.com/t5/Java-Development/To-make-an-HTTP-call-in-BB-7-1/m-p/2102801#M...

 

Let me try to repeat it a different way.

 

The BlackBerry has a variety of different connection paths it can use to connect from the BlackBerry to the Server.  You can tell the BlackBerry which connection path you want it to use by specifying the connection string, which is the suffix you add to the URL.  On a Simulator, if you tell it nothing, it will try to connect via the MDS connection, which will only work if you have the MDS Simulator running.  If you don't want to have the MDS Simulator running, then specify

";deviceside=true"

as a suffix to your URL.

 

I recommend that if you are testing on the Simulator, then add ";deviceside=true" to your URL, then make the connection.  Try this with htmlDemo and let us know what happens. 

Please use plain text.
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Re: Get string from url

In the simulator works, but how do the mobile phone?  both can be connected by wifi or 3G?

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get string from url

OK, then you just need to choose the correct connection string for the phone.  But I recommend that you do not do it that.  Instead look at using ConnectionFactory.  The documentation for this is not bad and there are many other Threads on this forum that discuss ConnectionFactory that should help.

 

Sorry this is not a trivial exercise on a BlackBerry. 

Please use plain text.