07-15-2009, 01:55 AM
|
#1 (permalink)
|
| New Member
Join Date: Jul 2009 Model: 7100T PIN: N/A Carrier: Programmer
Posts: 1
Post Thanks: 0 Thanked 0 Times in 0 Posts
| HELP Needed IN HttpConnection Problem Please Login to Remove! 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 = "URL Goes Here;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();
}
}
}
} |
| Offline
| |