0

/* Hi Iam developing an application where the BB app needs to post data to server. The Http connection works fine on Blackberry emulator, but when i try to test it on a real device the application cannot post data to server. the following is my code: */

package com.sims.datahandler;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

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

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

import com.sims.commonmethods.CommonMethods;
import com.sims.screens.MenuScreen;

/**
 * 
 * @author SaiKrishnaPawar
 *
 */
public class GPRSHandler extends Thread {

        private String data;
        private String url;
        private String msgKey;
        private String mobileNumber;

        public String sendGPRSRequest() {

             HttpConnection httpConn = null;
             DataOutputStream oStrm = null;
             DataInputStream is = null;

             byte[] resp = null;
             String responseData;
             try {

                 // Creating httpconnection object to handle GPRS request
                 httpConn = (HttpConnection) Connector.open(url);

                 httpConn.setRequestMethod(HttpConnection.POST);
                 httpConn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Confirguration/CLDC-1.0");
                 httpConn.setRequestProperty("Accept_Language", "en-US");
                 httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                 oStrm = httpConn.openDataOutputStream();

                 byte dataArray[] = (mobileNumber + "&" + msgKey + data).getBytes();

//               byte dataArray[] = (msgKey + data).getBytes();

                 CommonMethods.getSystemOutput("msg key and data:::"+mobileNumber + msgKey + data);

                 for (int i = 0; i < dataArray.length; i++) {
                     oStrm.writeByte(dataArray[i]);
                 }
                 DataInputStream din = httpConn.openDataInputStream();
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 int ch;
                 while ((ch = din.read()) != -1) {
                     baos.write(ch);
                 }

                 resp = baos.toByteArray();
                 responseData = new String(resp);
                 baos.close();
                 din.close();
                 httpConn.close();

                 return responseData.trim();

             } catch (IOException ex) {

                 CommonMethods.getSystemOutput("IO Exception in run method of gprs handler::" + ex.getMessage());

                 UiApplication.getUiApplication().invokeLater(new Runnable() {

                    public void run() {

                    int choice = Dialog.ask(Dialog.D_OK, "No Connectivity");

                    exitApp(choice);

                    }
                });

             } catch (NullPointerException nex) {

                 CommonMethods.getSystemOutput("NullPointerException:" + nex.getMessage());

             } catch (SecurityException e) {

                 CommonMethods.getSystemOutput("SecurityException:" + e.getMessage());

                 UiApplication.getUiApplication().invokeLater(new Runnable() {

                    public void run() {

                        Dialog.ask(Dialog.OK, "Security Exception");

                        UiApplication.getUiApplication().pushScreen(new MenuScreen());

                    }
                });

             } finally {
                 try {
                     if (is != null) {
                         is.close();
                     }
                     if (oStrm != null) {
                         oStrm.close();
                     }
                     if (httpConn != null) {
                         httpConn.close();
                     }

                 } catch (Exception ex) {

                     UiApplication.getUiApplication().invokeLater(new Runnable() {

                        public void run() {

                            Dialog.ask(Dialog.OK, "ERROR in While Connecting GPRS Connection");

                            UiApplication.getUiApplication().pushScreen(new MenuScreen());

                        }
                    });
                 }
             }

             return null;
        }

        public void setData(String data) {
            this.data = data;
        }

        public void setMsgKey(String msgKey) {
            this.msgKey = msgKey;
        }

        public void setUrl(String url) {
            this.url = url + ";deviceside=false";
        }

        public void setMobileNumber(String mobileNumber) {

            this.mobileNumber = mobileNumber;

        }

        private void exitApp(int choice) {

            System.exit(0);

        }

}
krisDrOid
  • 3,252
  • 3
  • 25
  • 36
  • How does it not post? Doesn't connect to your server? Bombs out? Returns bad data? – Marc B Dec 26 '11 at 07:26
  • Please set your url in this code before open the connection... – V.J. Dec 26 '11 at 07:43
  • You have to post some results you are getting from this code.. is the code throws any exception and are there any other issues?... From your code I think you are using MDS (;deviceside=false).. Also check your device connectivity. – Rupak Dec 26 '11 at 07:44
  • @Marc it doesn't connect to the server on real device – krisDrOid Dec 26 '11 at 08:18
  • @ BB expert: I am already setting the url at the bottom of code in setter method. – krisDrOid Dec 26 '11 at 08:20
  • But you have to call that function.. Without calling any function how can you run the functionality of the function.... – V.J. Dec 26 '11 at 08:35
  • hi... the problem has been solved...depending on the network provider the apn settings need to be enabled..this solved my problem. – krisDrOid Dec 30 '11 at 08:45

2 Answers2

1

Please add network extension in this line

 httpConn = (HttpConnection) Connector.open(url);

at the end of the url please check did you add url extension for suppose you are using wifi then you have to add

   httpConn = (HttpConnection) Connector.open(url+";interface=wifi");

this is working for interface if you want to other types of networks then just refer my answer here

"Tunnel Failed" exception in BlackBerry Curve 8520

Community
  • 1
  • 1
Govindarao Kondala
  • 2,862
  • 17
  • 27
1
   httpConn = (HttpConnection) Connector.open(url);

instead of this you can write//

   url = url + ";deviceside=false";
   httpConn = (HttpConnection) Connector.open(url);
V.J.
  • 9,492
  • 4
  • 33
  • 49
  • At the end of the code , if u can see i am already appending url with deviceside=false – krisDrOid Dec 26 '11 at 08:20
  • But you have to call that function.. Without calling any function how can you run the functionality of the function.... – V.J. Dec 26 '11 at 08:39
  • public void setUrl(String url) { this.url = url + ";deviceside=false"; } //where url is the local variable of class. iam passing the variable to this method and assigning it to a local variable. – krisDrOid Dec 26 '11 at 08:46
  • in that function iam assigning the url value to another variable "url". that variable iam using to open the socket connection. The code is perfectly fine, my only issue is it is not working on real device. – krisDrOid Dec 26 '11 at 09:19
  • Please add setUrl(url); This line before open the connection. Once try it...... – V.J. Dec 26 '11 at 09:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6133/discussion-between-krisdroid-and-bb-expert) – krisDrOid Dec 26 '11 at 09:34
  • hi... the problem has been solved...depending on the network provider the apn settings need to be enabled..this solved my problem – krisDrOid Dec 30 '11 at 08:45