BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 04-28-2008, 08:22 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 7100T
PIN: N/A
Carrier: do not know
Posts: 51
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Please Help in HttpConnection?

Please Login to Remove!

Hi every body,

I am new in BB programming and try to connect to internet through the http connection. When I ran this programme and clicked on the project icon using simulator Then it could not open the icon and could not display error as well.

Can any body help me where am I wrong.
Thanks and kind regards,

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.system.*;
import javax.microedition.io.*;
import java.io.*;

class ReadWebpageContents extends UiApplication{
HttpConnection connection = null;
InputStream stream = null;
StringBuffer b = new StringBuffer();

public void getReadWebpageContents(){

try {
connection = (HttpConnection)Connector.open
("http://www.cinq.net/bbtest.txt");
stream = connection.openInputStream();
int ch;
while ((ch = stream.read()) != -1) {
b.append((char) ch);
}
MainScreen screen=new MainScreen();
screen.add(new LabelField(b.toString()));
pushScreen(screen);

System.out.println("connection closed");
}catch (IOException e) {

}
}

public static void main(String args[]){
ReadWebpageContents rwc=new ReadWebpageContents();
rwc.getReadWebpageContents();
rwc.enterEventDispatcher();
}
}
Offline  
Old 04-28-2008, 08:57 AM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Model: 8100
PIN: N/A
Carrier: AT&T
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

You should Go through Run Java Applications from the menu in emulator
please can u help me
i am running J2me Applications in BB JDE but
i have included org.Kxml.*; for xml Parser but its giving me Symbol not found for xml variable please help me
thanks santosh
Offline  
Old 04-28-2008, 09:18 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 7100T
PIN: N/A
Carrier: do not know
Posts: 51
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default I am quite new in BB programming

Yes I did same. I went to the application and clicked on my project then it displayed nothing and nothing error.

Sorry, I am quite new in BB programming and do not know about the xml parsing in bb.
Thanks
Offline  
Old 04-28-2008, 09:36 AM   #4 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

it would be best if you would put your communication into an own thread as data communication is a blocking operation which should not be executed on the UI thread.
I'd suggest that you create and push the screen in the constructor of your class, start a networking thread and let the thread put the returned stuff into the label field (using invokelater to synchronize with the event thread).
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 04-28-2008, 10:38 AM   #5 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 7100T
PIN: N/A
Carrier: do not know
Posts: 51
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default vielen Dank!

Thank you very much for the suggestion. I followed your suggestion and made changes as following. But I found the same error again.
Can you give me the example because i am really new and doing effort for the whole day.
vielen Dank
Mit Freundlichen gruessen
Frankfurt am Main, Germany

class ReadWebpageContents extends UiApplication{
HttpConnection connection = null;
InputStream stream = null;
StringBuffer b = new StringBuffer();

ReadWebpageContents(){
MainScreen screen=new MainScreen();
String s=getReadWebpageContents();
screen.add(new LabelField(s));
pushScreen(screen);
}

public String getReadWebpageContents(){
try {
connection = (HttpConnection)Connector.open
("http://www.cinq.net/bbtest.txt");
stream = connection.openInputStream();
boolean firstItr = true;
int test=0;
int ch=0;
while ((ch = stream.read()) != -1) {
b.append((char) ch);
}

System.out.println("connection closed");
}catch (IOException e) {
}
return b.toString();
}

public static void main(String args[]){
ReadWebpageContents rwc=new ReadWebpageContents();
rwc.enterEventDispatcher();
}
}
Offline  
Old 04-28-2008, 11:48 AM   #6 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

especially if you are new to development it is important to keep things sorted. MVC

your code is quite stuffed and a bit hard to read.
I'd suggest that you make three java files in the beginning.
ReadWebpageContents.java : your application
ReadWebpageContentsScreen.java : your screen
ReadWebpageContentsWorker.java : your worker thread

to keep it simple i'll give the thread a reference to your screen. In "real" this would most likely be handled via interfaces/callbacks but let's keep it simple.

this would be your application class:
Code:
			
public class ReadWebpageContents extends UiApplication{

public static void main(String[] args) {
ReadWebpageContents theApp = new ReadWebpageContents();
theApp.enterEventDispatcher();
}

public ReadWebpageContents(){
ReadWebpageContentsScreen readWebpageContentsScreen = new ReadWebpageContentsScreen();
pushScreen(readWebpageContentsScreen);
new ReadWebpageContentsWorker(readWebpageContentsScreen).start();
}
}
Code:
public class ReadWebpageContentsScreen extends MainScreen{
private LabelField textField;

public ReadWebpageContentsScreen(){
textField = new LabelField();
add(textField);
}

public void setText(String text){
 textField.setText(text);
}

}
Code:
public class ReadWebpageContentsWorker extends Thread{

private ReadWebpageContentsScreen readWebpageContentsScreen;

public ReadWebpageContentsWorker(ReadWebpageContentsScreen readWebpageContentsScreen){
this.readWebpageContentsScreen=readWebpageContentsScreen;
}

public void run()
HttpConnection conn = null;
InputStream in = null;
conn = (HttpConnection) Connector.open("http://www.cinq.net/bbtest.txt");
if (conn.getResponseCode() == 200) {
	in = conn.openInputStream();
	byte[] data = new byte[200];
	int numBytes = in.read(data);
	StringBuffer responseBuffer = new StringBuffer();
	in.close();
	conn.close();
	for (int i = 0; i < numBytes; i++) {
	responseBuffer.append((char) data[i]);
	}
	String response = responseBuffer.toString();
	
UiApplication.getUiApplication().invokeLater(new Runnable() {
	public void run() {
	readWebpageContentsScreen.setText(result);						
	}
	});
}
(i didn't check all the brackets...)

hth,
simon
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 04-29-2008, 05:35 AM   #7 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 7100T
PIN: N/A
Carrier: do not know
Posts: 51
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

First, I am thanksfull to you that for the guideline of programming in BB.
The following code part from class was not clear to me ReadWebpageContentsWorker

UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
readWebpageContentsScreen.setText(result); }
});
I disabled this one and tried to run but it displayed only the blank screen after clicking on the project icon.

After then I used the following simple code as a thread

public class ReadWebpageContentsWorker extends Thread{
private ReadWebpageContentsScreen readWebpageContentsScreen;
public ReadWebpageContentsWorker(ReadWebpageContentsScree n
readWebpageContentsScreen){
this.readWebpageContentsScreen=readWebpageContents Screen;
}

public void run(){

HttpConnection connection = null;
InputStream stream = null;
StringBuffer b = new StringBuffer();
try{
connection = (HttpConnection)Connector.open
("http://www.cinq.net/bbtest.txt");
stream = connection.openInputStream();
int ch=0;
while ((ch = stream.read()) != -1) {
b.append((char) ch);
}
readWebpageContentsScreen.setText(b.toString());
System.out.println("connection closed");

}catch (IOException e) {
System.out.println("Exception"+e.getMessage());
}
}
}


It displayed me also the blank screen as a output. I am very confuesed. Please help me.
Thanks
Best regards
Frankfurt am Main
Offline  
Old 04-29-2008, 06:59 AM   #8 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

the invokelater is used to synchronize your networking thread with the event/ui thread

for a beginning all you need is your networking class. put some breakpoints into it and take a look if it receives anything.

the code i posted for this reads only 200 bytes but is copied directly from a project class that has been used many times.
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 04-29-2008, 07:52 AM   #9 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 7100T
PIN: N/A
Carrier: do not know
Posts: 51
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the nice help. I applied the break points but got the null in conn. I am running the following Thread as

public class ReadWebpageContentsWorker extends Thread{
String response;
private ReadWebpageContentsScreen readWebpageContentsScreen;

public ReadWebpageContentsWorker(ReadWebpageContentsScree n readWebpageContentsScreen){
this.readWebpageContentsScreen=readWebpageContents Screen;
}

public void run(){
HttpConnection conn = null;
InputStream in = null;
try{
conn = (HttpConnection) Connector.open("http://www.cinq.net/bbtest.txt");
//if (conn.getResponseCode() == 200) {
in = conn.openInputStream();
byte[] data = new byte[200];
int numBytes = in.read(data);
StringBuffer responseBuffer = new StringBuffer();
in.close();
conn.close();
//}

for (int i = 0; i < numBytes; i++) {
responseBuffer.append((char) data[i]);
}

response = responseBuffer.toString();
readWebpageContentsScreen.setText(response);
}catch (IOException e) {
System.out.println("Here is exception"+e.getMessage());
}

UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
readWebpageContentsScreen.setText(response);
}
}
);
}
}
Offline  
Old 04-29-2008, 09:21 AM   #10 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

i just copied your source into a new eclipse project, simplified it a bit, added the stuff in my JDE and ran it. got "SUCCESS" as my output, i'd guess that's the content of your txt-file.

be sure to start your MDS simulator before launching the program.

Code:
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class ReadWebpageContentsWorker extends Thread {

	public void run() {
		String response;
		HttpConnection conn = null;
		InputStream in = null;
		try {
			conn = (HttpConnection) Connector
					.open("http://www.cinq.net/bbtest.txt");
			in = conn.openInputStream();
			byte[] data = new byte[200];
			int numBytes = in.read(data);
			StringBuffer responseBuffer = new StringBuffer();
			in.close();
			conn.close();
			for (int i = 0; i < numBytes; i++) {
				responseBuffer.append((char) data[i]);
			}
			response = responseBuffer.toString();
			System.out.println(response);
		} catch (IOException e) {
			System.out.println("Here is exception" + e.getMessage());
		}
	}
}
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 04-29-2008, 12:05 PM   #11 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2008
Model: 7100T
PIN: N/A
Carrier: do not know
Posts: 51
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default You are really Nette Man.

Thank you very much, you are really Nette Man.
I have one question. I am running the application on BlackBerry JRE 4.3.0.
But I got the blank screen. Should I need to give the reference of the proxy server. Because I am accessing internet through proxy.

Thanks
Kind kind regards,
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.