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
New Developer
arnosepe
Posts: 1
Registered: ‎12-01-2008
My Device: Not Specified

Make an HTTP Connection to get a Content of URL

Hi,

 

I try to get a Content of URL with a http Connection, but, I try many solution, but nothing work.

 

My last code is :

 

StreamConnection c = null;
InputStream s = null;

 

try {
   System.out.println("--------  Step 1 : ---------");
   c = Connector.open("My URL");
   System.out.println("--------  Step 2 ---------");
   s = c.openInputStream();
   int ch;
   while ((ch = s.read()) != -1) {
      System.out.println((char)ch);
   }

} catch (IOException e)

{

...

}

 

 

In the debugger, I see Step One and Step Two, but, my url page log connections and, there are no log from me. So I it doesn't work.

 

If you get another way. it's ok too

 

Thanks for Your help

 

Arnaud SEPEROUMAL

EPITA

Please use plain text.
Administrator
MSohm
Posts: 14,416
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Make an HTTP Connection to get a Content of URL

I'm not sure I understand your issue.  But you can refer to the httpdemo included with the BlackBerry JDE for an example on making an HTTP connection.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
New Developer
criccomini
Posts: 26
Registered: ‎12-03-2008
My Device: Not Specified

Re: Make an HTTP Connection to get a Content of URL

Have you enabled your MDS server? In eclipse, I was unable to get a connection to the internet until MDS was running.

 

I also wrote an HTTP client that I know works. Check it out at: http://riccomini.name/Topics/Mobile/BlackBerry/BlackBerryHTTPClient/

 

Code follows:

 

import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

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

try {
StreamConnection s = (StreamConnection)Connector.open(url);

InputStream input = s.openInputStream();

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));
}

response = raw.toString();

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

return response;
}
}

 

BlackBerry/Mobile Enthusiast - http://www.riccomini.name
Please use plain text.
Developer
satish4ru
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified
My Carrier: developer

Re: Make an HTTP Connection to get a Content of URL

i am using the above class in my project  but here i am passing my url but i am getting any thing from this method......... it just returning the empty value please solve my problem............

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

Re: Make an HTTP Connection to get a Content of URL

Have you stepped through the code to see what happens?  I suggest you put a System.out in the Exception catch clause too. 

Please use plain text.
Developer
satish4ru
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified
My Carrier: developer

Re: Make an HTTP Connection to get a Content of URL

hi peter i tried like that only but i didnt get any exception i will send my code you i kept println statement in the while loop also but i am not getting the string from url ... 

package com.logictreeit.party;

import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

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

		try {
			StreamConnection s = (StreamConnection)Connector.open(url);

			InputStream input = s.openInputStream();

			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");
			System.out.println(raw+"this is raw data");
			
			response = raw.toString();

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

		return response;
	}
}

 the value value of response was not changing.....

 

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

Re: Make an HTTP Connection to get a Content of URL

Have you stepped through the code?  What line does it get too? 

 

I suggest you try this sample code which is much more robust:

http://supportforums.blackberry.com/t5/Java-Development/Sample-HTTP-Connection-code-and-BIS-B-Access...

Please use plain text.
Contributor
nileshpawate
Posts: 28
Registered: ‎05-03-2011
My Device: 9530
My Carrier: cell

Re: Make an HTTP Connection to get a Content of URL

the only solution for this is

 

Run your MDS Service the application will work

 

download Blackberry Emial and MDS simulator and install it..

before running the aplication run the MDS Service  

 

You will get the Solution.

 

Thanx,

Nilesh :Rockon:

 

Please use plain text.
Developer
sheeluwiki
Posts: 254
Registered: ‎03-15-2011
My Device: simulator
My Carrier: simulator

Re: Make an HTTP Connection to get a Content of URL

Are you able to open the URL you have used in the browser of your simulator?

Have you turned the MDS on?

 

Try using HttpConnection..

------------------------------------------------------------------------------------------------
Click "Accept as Solution" if post was what you needed. Give kudos if you feel response is helpful.

Please use plain text.
Contributor
ieee488
Posts: 13
Registered: ‎05-15-2011
My Device: none

Re: Make an HTTP Connection to get a Content of URL

[ Edited ]

criccomini wrote:

I also wrote an HTTP client that I know works. Check it out at: http://riccomini.name/Topics/Mobile/BlackBerry/BlackBerryHTTPClient/

 

Code follows:

 

import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

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

try {
StreamConnection s = (StreamConnection)Connector.open(url);

InputStream input = s.openInputStream();

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));
}

response = raw.toString();

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

return response;
}
}

 


Thank you. This worked for me. I am using JDE 4.5.0.7.

 

I found that the default profile in Preferences->Simulator->General doesn't have Mobile Data System Connection Service MDS-CS enabled by default, so I just created another one.

 

Can you tell me why you chose to read 256 bytes at a time?

 

 

Please use plain text.