tags:

views:

586

answers:

4

I have developed one simple application in J2ME. Application just do simple HttpConnection and make only request. Here is the code for that :

 public void run() {
    System.out.println("Inside saveData");
    HttpConnection hc = null;
    OutputStream dout = null;
   try {
        System.out.println("custName = " + custName);
        System.out.println("prodName = " + prodName);
        System.out.println("qty = " + qty);

        hc = (HttpConnection) Connector.open("http://www.sufalamtech.com/demo/mobile/test.php?custName=" + custName + "&prodName=" + prodName + "&qty=" + qty);
        //hc = (HttpConnection) Connector.open("http://www.sufalamtech.com/demo/mobile/test.php?custName=Custtt51&prodName=Proddd52&qty=53");
        //hc = (HttpConnection) Connector.open("http://www.sufalamtech.com/demo/mobile/test.php?custName="+custName+"&prodName="+prodName+"&qty="+qty);

        hc.setRequestProperty("User-Agent", "Profile/MIDP-2.1 Configuration/CLDC-1.1");
        hc.setRequestMethod(HttpConnection.GET);
        dout = hc.openOutputStream();

    } catch (Exception e) {
        System.out.println("Error... = " + e);
    } finally {
        try {
            if (dout != null) {
                dout.close();
            }

            if (hc != null) {
                hc.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

This works fine in PC (simulator). But when i am deploying .jar file on my Nokia 5310, it is not returning anything from HttpConnection.

Actually i don't want to receive any data from URL. I just wanna send request to my URL. Else will be done by that URL only... My Application is works fine in Nokia 3110 Classic. But not working on Nokia 5310. Do you have any suggestion plz ?

Thanks...

+1  A: 

You need to use the input stream of the connection, not the output stream. Somehting like this (untested).

    if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
        int length = (int)hc.getLength();
        InputStream is = hc.openInputStream();
        content = new byte[length];
        int read = 0;
        while (read < length) {
            int r = is.read(content, read, length - read);
            if (r < 0) {
                break;
            }
            read += r;
        }
        is.close();
    }
    hc.close();
Zed
Actually i don't want to receive any data from URL. I just wanna send request to my URL. Else will be done by that URL only... My Application is works fine in Nokia 3110 Classic. But not working on Nokia 5310. Do you have any suggestion plz ?
Nirmal
You sure that it's not just a permission issue? Starting a data connection is often restricted. My phone keeps asking me every time I connect.
wvdschel
Yes, that is not a permission issue. In fact application is running on Nokia 3110 perfectly.. So, it has to be some other problem.. Plz suggest any solutions if you have..
Nirmal
+1  A: 

The problem is that on some implementations, your application might not even send the request until you have invoked the connection.getResponseCode() method. Try invoking the getResponseCode() method before opening the InputStream or OutputStream

Ram
Hello Ram. I have tried out to call a getResponseCode() function. But still my problem is same that i can't get proper execution in Nokia 5310. But i am getting perfect result in Nokia 3110 Classic. Plz suggest any solution if you have... Thanks..
Nirmal
#1 Also don't try to override the User-Agent header. Because some devices are known to throw exceptions when you set User-Agent header. Try commenting that line.#2 When you are making a GET request you normally open an InputStream and not an OutputStream
Ram
Hello Ram, thanks for your reply.. I have made both the changes but still not getting proper output.. I don't know wt to do... is there anyway to debug application directly in mobile ?
Nirmal
Maybe you can show an alert in case you catch an exception or in the finally block. Label the messages appropriately so that you know where the error happens when the alert shows up.Could you also explain, what you are expecting at the server end to happen and what data plan are you using in your mobile phone. Are you using a wap connection or gprs connection to hit that URL?
Ram
Nirmal
What is the response code you get and can you sniff the network packets at the server end to see if the request actually reaches the server or not
Ram
A: 

I have the same problem on Nokia E Series (E51) the code works perfectly in the Sun Simulator , however try setting your application/MIDlets defalut access point in the Application Manager this might slove your problem. Go to Installations -> App. Manager -> Open Your Apps Name, Change Seeting -> Choose Access Point for you App

ary
A: 

I too had this same problem , when i was accessing the application through 3110 , 5310. Actually this problem is not because of application . It is because of connectivity which we are using in mobile. For solving this problem we will have to create access point in internet setting on mobile. or we will have to contact operator for activating the mobile office settings.

Thanks

pavun_cool