Post ConnectionRequest Json

170 views
Skip to first unread message

Mario Barón

unread,
Dec 7, 2014, 10:05:50 AM12/7/14
to codenameone...@googlegroups.com
Hey guys,


I'm having a rather weird error with Post type ConnectionRequest. I'm making a normal Post ConnectionRequest through my app, in which I send some data in which some short and long fields are included. One of those fields is a String that may have a maximum length of 255. When I execute this call through the CN1 simulator it goes on regularly, the entire data gets trough and it's saved on a database. Nevertheless, when I install the app on my android device the field with 255 characters is cut short to 100 on some devices, or 102 or some others I tried on.

The problem is not within the Web services or the server since, like I was telling you, on the simulator the entire data gets through normally, and I even went ahead and extracted the JSON created and made the request through a program named Fiddler 2 and it went on normally, by the way, I'm printing out the JSON created right before making the ConnectionRequest and the data is all there. Can you please help me wth this?


Thanks

Shai Almog

unread,
Dec 7, 2014, 12:50:40 PM12/7/14
to codenameone...@googlegroups.com
Hi,
how do you use the connection request?

Mario Barón

unread,
Dec 8, 2014, 8:31:52 AM12/8/14
to codenameone...@googlegroups.com
Hey Shai, 

thanks for your response. This is an example of exactly how I'm calling this Post WS:

public void callWS() {

   Hashtable h = new Hashtable();
   Hashtable h2 = new Hashtable();

   

   h2.put("Field1_1", field1_1);

   h2.put("Field1_2", field1_2);

   h2.put("Field1_3", field1_3);

   h.put("Field1", h2);

   h.put("User", user);

   h.put("Password", password);

   

   final String payload = Result.fromContent(h).toString();

   System.out.println("payload: " + payload);

    ConnectionRequest request = new ConnectionRequest() {

     String respuesta = "";

     

     @Override

     protected void postResponse() {

       //TODO do something

     }

     

     @Override

     protected void readResponse(InputStream input) throws IOException {

       String res = Util.readToString(input);

       //TODO do something

     }

     

     @Override

     protected void buildRequestBody(OutputStream os) throws IOException {

       os.write(payload.getBytes("UTF-8"));

     }

     

     @Override

     protected void handleErrorResponseCode(int code, String message) {

     }

     

     @Override

     protected void handleException(Exception e) {

       e.printStackTrace();

     }

   };

   

   request.setTimeout(60000);

   request.setContentType("application/json");

   request.setUrl(Utilidades.getIpServiciosWeb());

   request.setPost(true);

   request.setHttpMethod("POST");

   

   NetworkManager netMgr = NetworkManager.getInstance();

   

   netMgr.setTimeout(60000);

   netMgr.addToQueueAndWait(request);

 }



Where some of those fields in the hashtable can be some int ids or small Strings, and one of them is a String observation that can be up to 255 chars long hope this can be of help.


Thanks.

Shai Almog

unread,
Dec 8, 2014, 10:48:47 AM12/8/14
to codenameone...@googlegroups.com
Try this:
    protected void buildRequestBody(OutputStream os) throws IOException {

           
if(shouldWriteUTFAsGetBytes()) {
                os
.write(payload.toString().getBytes("UTF-8"));
           
} else {
               
OutputStreamWriter w = new OutputStreamWriter(os, "UTF-8");
                w
.write(payload.toString());
           
}
   
}


Mario Barón

unread,
Dec 10, 2014, 7:11:14 AM12/10/14
to codenameone...@googlegroups.com
Hey Shai, 

thank you that worked perfectly.



On Sunday, December 7, 2014 10:05:50 AM UTC-5, Mario Barón wrote:
Reply all
Reply to author
Forward
0 new messages