10-18-2012 09:09 AM
I use the following code to connect to my server to upoad an image file to my server. It works well when I connect through Wifi. But when I disconnect wifi and turn on my mobile network (Blackberry Internet plan), it doesn't upload the image. Can anyone help me in this case? Thanks in advance.
Here is my code:
public static String getString()
{
String connectionString = null;
String uid = null;
// Wifi
if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
{
connectionString = ";interface=wifi";
}
else
{
ServiceBook sb = ServiceBook.getSB();
net.rim.device.api.servicebook.ServiceRecord[] records = sb.findRecordsByCid("WPTCP");
for (int i = 0; i < records.length; i++) {
if (records[i].isValid() && !records[i].isDisabled()) {
if (records[i].getUid() != null &&
records[i].getUid().length() != 0) {
if ((records[i].getCid().toLowerCase().indexOf("wptcp ") != -1) &&
(records[i].getUid().toLowerCase().indexOf("wifi") == -1) &&
(records[i].getUid().toLowerCase().indexOf("mms") == -1) ) {
uid = records[i].getUid();
break;
}
}
}
}
if (uid != null)
{
connectionString = ";deviceside=true;ConnectionUID="+uid;
}
else
{
// the carrier network
if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
{
String carrierUid = getCarrierBIBSUid();
if (carrierUid == null) {
// Has carrier coverage, but not BIBS. So use the
// carrier's
// TCP network
connectionString = ";deviceside=true";
} else {
// otherwise, use the Uid to construct a valid carrier
// BIBS
// request
connectionString = ";deviceside=false;connectionUID="
+ carrierUid;
}
}
//(BlackBerry Enterprise Server)
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS)
{
//connectionString = ";deviceside=false";
connectionString = ";deviceside=false;ConnectionTimeout=54321";
}
// If there is no connection available abort to avoid bugging
// the user unnecessarily.
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
}
// In theory, all bases are covered so this shouldn't be
// reachable.
else {
//no other options found, assuming device
connectionString = ";deviceside=true";
}
}
}
return connectionString;
}
private static String getCarrierBIBSUid() {
try {
net.rim.device.api.servicebook.ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;
for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
if (records[currentRecord].getCid().toLowerCase().equ als(
"ippp")) {
if (records[currentRecord].getName().toLowerCase()
.indexOf("bibs") >= 0) {
return records[currentRecord].getUid();
}
}
}
} catch (Exception e) {
System.out.println("Exception"+e.getMessage());
}
return null;
}
And I use it like this:
StringBuffer connectionStr=new StringBuffer("http://www.myserver.com/upload_img.php");
connectionStr.append(getString());
HttpConnection conn = (HttpConnection) Connector.open(connectionStr.toString(), Connector.READ_WRITE);
10-18-2012 09:38 AM
10-19-2012 12:46 AM
Thanks simon for helping me again. What really happens is that, using the code I posted before, when the Wifi is turned off and mobile network is turned on, the application first uses my phone balance (ie. the money I have recharged it with), it does not use the internet plan I took from my service provider.
And I was not aware of ConnectionFactory, will use it and then tell you the result.
Thanks again.
10-19-2012 02:52 AM
Hi Nikita,
Can you please detail of your webservice, i will do code then will put it here.
Thanks
Pawan
10-19-2012 04:05 AM
10-20-2012 06:08 AM - edited 10-20-2012 06:46 AM
I used ConnectionFactory now and it shows this exception when it tries to open InputStream:
Exception : Sending connection timed out after ~120000ms
My code to get connection:
final HttpConnection getConnection(String urlT) throws Exception {
connectionFactoryObjL = new ConnectionFactory();
connectionFactoryObjL.setPreferredTransportTypes(n ew int[] {
TransportInfo.TRANSPORT_MDS,
TransportInfo.TRANSPORT_BIS_B,
TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_TCP_CELLULAR,
TransportInfo.TRANSPORT_WAP2,
TransportInfo.TRANSPORT_WAP
});
connectionFactoryObjL.setAttemptsLimit(2);
connectionFactoryObjL.setTimeLimit(5000);
connectionFactoryObjL.setTimeoutSupported(true);
ConnectionDescriptor connectionDescriptor=connectionFactoryObjL.getConn ection(urlT);
HttpConnection connection=(HttpConnection) connectionDescriptor.getConnection();
return connection;
}
Code for sending image:
public synchronized void sendImg(String url){
final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";
HttpConnection conn=null;
try{
byte[] imagedata=getFileBytes(new StringBuffer("file:///store/home/user/_"+filename+ ".jpg"));
//conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
conn=ConnectToServer.getInstance().getConnection(u rl);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEAD ER_CONTENT_TYPE,
HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_ DATA
+ ";boundary=" + BOUNDARY);
conn.setRequestProperty(HttpProtocolConstants.HEAD ER_CONTENT_LENGTH,
String.valueOf(imagedata.length));
conn.setRequestProperty("x-rim-transcode-content", "none");
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream finalOut = conn.openOutputStream();
String newLine = "\r\n";
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(BOUNDARY.getBytes());
out.write(newLine.getBytes());
String contDisp = "Content-Disposition:form-data; name=\"uploadedfile\";filename=\"_"+getCurrFilenam e()+".jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String type = "Content-Type:image/jpeg";
out.write(contDisp.getBytes());
out.write(newLine.getBytes());
out.write(type.getBytes());
out.write(newLine.getBytes());
out.write(contEnc.getBytes());
out.write(newLine.getBytes());
out.write(newLine.getBytes());
out.write(imagedata);
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(BOUNDARY.getBytes());
out.write("--".getBytes());
out.write(newLine.getBytes());
finalOut.write(out.toByteArray());
System.out.println("Bytes Written");
out.flush();
out.close();
finalOut.flush();
finalOut.close();
InputStream instream=conn.openInputStream(); //exception occurs here
int ch=0;
StringBuffer buffesr=new StringBuffer();
while((ch=instream.read())!=-1)
{
buffesr.append((char)ch);
}
System.out.println("Server Response : "+new String(buffesr));
}
catch (Exception e) {
// TODO: handle exception
System.out.println("===Exception : "+e.getMessage());
}
finally{
try {
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("===IOException : "+e.getMessage());
}
}
}
I call it like this:
StringBuffer connectionStr=new StringBuffer("http://www.myserver.com/upload_img.php?folder=");
connectionStr.append(getFolderName());
sendImg(connectionStr.toString());
Please help..
10-22-2012 04:01 AM
10-25-2012 05:47 AM - edited 10-25-2012 05:53 AM
Sorry for replying late. Removed the BIS option but still getting the same exception:
Exception : Local connection timed out after ~ 120000
What to do?
10-25-2012 05:49 AM
Hi Nikita
Can you please give me your url. so that i may test at my end.
And, tell me also how to check if the image is properly uploaded on server.
Thanks
Pawan
10-27-2012 06:56 PM
In your code, before you try to open the input stream, can you please retrieve and get the Http return code. Make sure this is OK (200), before you continue.
Why are you trying to retrieve input data? Does your POST service really return some data when it has completed processing a POST?