10-11-2011 07:52 AM
Hi, im a new blackberry developer, i have no problem when using the connection using wifi but when i off my wifi and try to connect using 3g sometimes the data is send but sometimes it dont...what could possibly error in this case?
try {
String _postData = "";
OutputStream os;
//the preferred network is already arranged here
int[] preferredTransportTypes = {
TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_WAP2,
TransportInfo.TRANSPORT_TCP_CELLULAR};
ConnectionFactory factory = new ConnectionFactory();
factory.setPreferredTransportTypes(preferredTransp ortTypes);
factory.setAttemptsLimit(3);
factory.setConnectionTimeout(10000);
//factory.setTimeLimit(10000);
//modify this
final ConnectionDescriptor connDescriptor = factory.getConnection("http://url here");
if(connDescriptor==null){
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Invalid Network. Please check your network set up.");
}
});
}else{
HttpConnection http = (HttpConnection) connDescriptor.getConnection();
//https.setRequestMethod(HttpsConnection.POST);
//byte[] postDataBytes = _postData.getBytes();
http.setRequestMethod(HttpConnection.POST);
http.setRequestProperty("User-Agent","Profile/MIDP -2.0 Configuration/CLDC-1.0");
http.setRequestProperty("Content-Language", "en-US");
http.setRequestProperty("Content-Type","applicatio n/x-www-form-urlencoded");
//https.setRequestProperty("Content-length", Integer.toString(_postData.length()));
byte [] postDataBytes = _postData.getBytes("UTF-8");
os = http.openOutputStream();
os.write(postDataBytes);
os.flush();
os.close();
int state = http.getResponseCode();
final int state2 = state;
if(state==HttpConnection.HTTP_OK || state==HttpConnection.HTTP_BAD_GATEWAY){
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
//original
//Dialog.alert("SOS sent. Our customer care personnel will contact you shortly.");
Dialog.alert("SOS sent. This is the response code : " + state2);
//Dialog.inform("SOS sent. Our customer care personnel will contact you shortly.");
}
});
} else {
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
//Dialog.alert("Failed to send SOS .");
Dialog.alert("Failed to send SOS. Please try again.This is the response code : " + state2);
}
});
}
}
} catch(IOException e) {Dialog.inform(e.getMessage());}im using blackberry 9700 with os 6
10-11-2011 08:24 AM
10-11-2011 10:11 AM