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
cool123
Posts: 3
Registered: ‎07-15-2009
My Device: Not Specified
Accepted Solution

HELP Needed IN HttpConnection Problem

Hi I'm a Newbie to Blackberry Development..I'm try to read the Data from the URL through HttpConnection. The below is my code. When i invoke through simulator the code, The following code does not executes.. System.out.println("MDS getResponseCode"+c.getResponseCode()); There is no exception too.. Pls help Whats wrong in my code..

Thanks in Advance -Poy


import java.io.IOException;
import java.io.InputStream;
import java.lang.String;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;


// Main class
public class HttpCxn extends UiApplication {

// Starting point
public static void main(String[] args) {
HttpCxn httpCxn = new HttpCxn();
httpCxn.enterEventDispatcher();
}

// Calls initial screen
public HttpCxn() {
pushScreen(new HttpCxnScreen());
}

}

class HttpCxnScreen extends MainScreen {

// Declare my initial variables

ButtonField submitButton = new ButtonField("Submit", ButtonField.CONSUME_CLICK);
HttpCxnScreen screen = this;

// Main screen, including entries for Character/Server names
public HttpCxnScreen() {

// Create Label for title for main screen
LabelField mainTitle = new LabelField("HTTP CONNECTION", LabelField.ELLIPSIS
| LabelField.USE_ALL_WIDTH);
// Set title to above label
setTitle(mainTitle);
// Add other items to screen

add(submitButton);

// Create functionality of submit button
submitButton.setChangeListener(new FieldChangeListener() {

public void fieldChanged(Field field, int context) {
Thread postnameThread = new Thread(new ServerConnection());
postnameThread.start();
}

});

}


// http connection class
class ServerConnection extends Thread {


String url = "http://172.24.30.27:9091/demo/index.jsp;deviceside=false";



// Method called by submit button
public ServerConnection() {
System.out.println("test");
}

public void run() {
HttpConnection c = null;
InputStream is = null;
String response;
int rc;

try {
StringBuffer buffer = new StringBuffer();
System.out.println("URL"+url);
c = (HttpConnection)Connector.open(url);
System.out.println("AZtest"+c);//my debugger does not passed this line
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
System.out.println("MDS getResponseCode"+c.getResponseCode());
rc = c.getResponseCode();
System.out.println("MDS CHECK"+rc);
if (rc != HttpConnection.HTTP_OK) {
System.out.println("HTTP_OK"+rc);
throw new IOException("HTTP response code: " + rc);
}

is = c.openInputStream();

// Get the ContentType
String type = c.getType();

// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
} else {

int ch;

while ((ch = is.read()) != -1) {
ch = is.read();
buffer.append((char) ch);
}
response = buffer.toString();


}
} catch (Exception e) {

e.printStackTrace();

}
}
}
}

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

Re: HELP Needed IN HttpConnection Problem

Do you mean that particular line of code is skipped or is the application blocking on that line?  What BlackBerry Smartphone model and BlackBerry handheld software version are you testing on?  You can find this under Options, About on the BlackBerry Smartphone.
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.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: HELP Needed IN HttpConnection Problem

This post might help.



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
New Developer
cool123
Posts: 3
Registered: ‎07-15-2009
My Device: Not Specified

Re: HELP Needed IN HttpConnection Problem

Hi MSohm and Ted_Hopp, 

Thank for both you for spending the valuable time.

The problem is with enabling  MDS services.

I was using BlackBerry JDE Plug-in for Eclipse,with enabled MDS Services .But it was not working properly.Then I have deployed my sample in BlackBerry JDE and invoked MDS services.It works fine Now.

 

Thanks,

Poy

Please use plain text.