2

I have developed one simple application in J2ME. Application just does simple HttpConnection and makes 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 want to send request to my URL. Else will be done by that URL only... My Application works fine in Nokia 3110 Classic. But it is not working on Nokia 5310. Do you have any suggestion?

gnat
  • 6,213
  • 108
  • 53
  • 73
Nirmal
  • 4,789
  • 13
  • 72
  • 114

3 Answers3

1

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
  • 57,028
  • 9
  • 76
  • 100
  • 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 Aug 25 '09 at 10:21
  • 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 Aug 25 '09 at 12:11
  • 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 Aug 26 '09 at 09:16
1

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

Prabhu R
  • 13,836
  • 21
  • 78
  • 112
  • 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 Aug 26 '09 at 12:55
  • #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 – Prabhu R Aug 26 '09 at 13:27
  • 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 Aug 26 '09 at 13:49
  • 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? – Prabhu R Aug 26 '09 at 14:16
  • I just wanna send 3 parameters as URLString from user i.e. product name, customer name & qty. Rest work will be done by my test.php file. I am using gprs connection for hit the URL. – Nirmal Aug 26 '09 at 14:45
  • 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 – Prabhu R Aug 27 '09 at 12:56
0

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
  • 1