J2me app not working on mobile

I have developed one simple application in J2ME. The app just keeps it simple HttpConnection

and only makes the request. Here's 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 great on PC (simulator). But when I deploy the .jar file on my Nokia 5310, it returns nothing from the HttpConnection.

I actually don't want to receive data from the url. I just want to send a request to my url. Else will only execute this URL ... My application works fine in Nokia 3110 Classic. But it doesn't work on Nokia 5310. Do you have any suggestions?

+2


source to share


3 answers


The problem is that, in some implementations, the application may not even send a request until you call the connection.getResponseCode () method. Try to call getResponseCode () method before opening InputStream or OutputStream



+1


source


You need to use the input stream of the connection, not the output stream. Something 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(); 

      

+1


source


I have the same problem for Nokia E Series (E51) that the code works fine in Sun simulator, however try to set your application / MIDlets hotspot in application manager, it might affect your problem. Go to Settings β†’ Application. Manager -> Open your app name, change location -> Select hotspot for your app

0


source







All Articles