Skip to main content

LWUIT IO + JSON + POST

4 replies [Last post]
essequadra
Offline
Joined: 2011-09-09
Points: 0

Hi,
scuse me for my bad English.

I must call a WebService and I pass it a parameter in POST. This code in JAVA ME works perfectly but I don't understand how I must write this using LWUIT IO classes

<br />
        StringBuffer stringBuffer = new StringBuffer();<br />
        String strContenentType = "application/json";<br />
        String strContentLength = String.valueOf(strParameter.length());<br />
        String strConnection = "close";<br />
        InputStream is = null;<br />
        OutputStream os = null;<br />
        HttpConnection hc = null;<br />
        try {<br />
            hc = (HttpConnection) Connector.open(url);<br />
            hc.setRequestMethod("POST");<br />
            hc.setRequestProperty("Content-Type", strContenentType);<br />
            hc.setRequestProperty("Content-Length", strContentLength);<br />
            hc.setRequestProperty("Connection", strConnection);</p>
<p>            os = hc.openOutputStream();<br />
            os.write(strParameter.getBytes());<br />
            os.flush();<br />
            os.close();<br />
            int rc = hc.getResponseCode();<br />
            if (rc != 200) {<br />
                Dialog.show("Errore " + rc, "Il server ha risposto: " + hc.getResponseMessage(), "OK", null);<br />
            } else {<br />
                is = hc.openInputStream();<br />
                int ch;<br />
                while ((ch = is.read()) != -1) {<br />
                    stringBuffer.append((char) ch);<br />
                }<br />
            }<br />
        } catch (SecurityException se) {<br />
            Dialog.show("Errore", "security exception:" + se, "OK", null);<br />
        } catch (NullPointerException npe) {<br />
            Dialog.show("Errore", "null pointer excception:" + npe, "OK", null);<br />
        } catch (IOException ioe) {<br />
            Dialog.show("Errore", "io exception:" + ioe, "OK", null);<br />
        }<br />
        try {<br />
            hc.close();<br />
            is.close();<br />
        } catch (Exception e) {<br />
            Dialog.show("Errore", "Error in MostActivePareser Connection close:" + e, "OK", null);<br />
            e.printStackTrace();<br />
        }</p>
<p>        String jsonData = stringBuffer.toString();<br />
        Dialog.show("Resultato", "Questo: " + jsonData, "OK", null);</p>
<p>

I tried this code but not work (400 - bad request):

</p>
<p>        NetworkManager.getInstance().start();</p>
<p>        ConnectionRequest myRequest = new ConnectionRequest();<br />
        myRequest.setUrl(url);<br />
        myRequest.setPost(true);<br />
        myRequest.setContentType("application/json");<br />
        myRequest.addArgument("Request", strParameter);</p>
<p>        myRequest.addResponseListener(new ActionListener() {</p>
<p>            public void actionEvent(ActionEvent ev) {<br />
                NetworkEvent n = (NetworkEvent) ev;</p>
<p>// gets the data from the server as a byte array...<br />
                byte[] data = (byte[]) n.getMetaData();<br />
                Dialog.show("FATTO", "Ha funzionato", "ok", null);<br />
            }</p>
<p>            public void actionPerformed(ActionEvent ae) {<br />
                Dialog.show("FATTO", "Ha funzionato", "ok", null);<br />
            }<br />
        });<br />
        NetworkManager.getInstance().addToQueue(myRequest);</p>
<p>    }</p>
<p>

can anyone help me?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
essequadra
Offline
Joined: 2011-09-09
Points: 0

Hi,

thanks for help. I have solved:

   NetworkManager.getInstance().start()
   [...]
   ConnectionRequest myRequest = new ConnectionRequest()
   {
       protected void buildRequestBody(OutputStream os) throws IOException
       {
           os.write(strParameter.getBytes());
           os.flush();
           os.close();
       }
       protected void readResponse(InputStream input) throws IOException  {
          StringBuffer stringBuffer = new StringBuffer();
          int ch;
          while ((ch = input.read()) != -1) {
             stringBuffer.append((char) ch);
          }
          [...]
        }
    };
   [...]

   myRequest.setUrl(url);
   myRequest.setPost(true);
   myRequest.setContentType(&quot;application/json&quot;);
   myRequest.addArgument(&quot;Request&quot;, strParameter);
   [...]

   NetworkManager.getInstance().addToQueue(myRequest);
vprise
Offline
Joined: 2003-11-07
Points: 0

It seems from a quick glance of the code that you are using addArgument incorrectly, maybe some other issues I missed. addArgument() accepts the key and value, you need to get the string before it was encoded which is generally how you would have the data. I'm also guessing you have more than one argument for the request.

essequadra
Offline
Joined: 2011-09-09
Points: 0

Hi, thanks for the response.
I don't understand how to call my WebService using ConnectionRequest of LWUIT.
Request and response of WebService are json objects. Using JavaME I pass parameter using

hc = (HttpConnection) Connector.open(url);
[...]
os = hc.openOutputStream();
os.write(strParameter.getBytes());
os.flush();
os.close();

How do I pass this parameter with LWUIT?

vprise
Offline
Joined: 2003-11-07
Points: 0

I'm guessing you are trying to add an argument in post. Arguments in post are added automatically in the correct way just use the connection request addArgument and give it the key/value of the argument. So if your argument is:

data={'jsonString'}

Then just use addArgument("data", "{'jsonString'}");