How to send/get data to/from Servlet?

21 views
Skip to first unread message

samer.als...@gmail.com

unread,
Jul 10, 2016, 2:27:00 PM7/10/16
to CodenameOne Discussions
Hello
I tried to connect with Servlet but the I don't how!!, I searched on tutorials to send and received data but no positive result.
this is my code ::
@Override
   
protected void onMain_ButtonAction(Component c, ActionEvent event) {
       
try {
           
InfiniteProgress inf = new InfiniteProgress();          
           
Dialog progress = inf.showInifiniteBlocking();
           
MultipartRequest request = new MultipartRequest();
           
            request
.setUrl("http://127.0.0.1:7101/TestServlet-SerlvletTest-context-root/servlet1");
           
            request
.setDisposeOnCompletion(progress);
           
NetworkManager.getInstance().addToQueue(request);
       
}
       
catch (Exception ex) {
           
System.out.println(ex.getMessage());
       
}
   
}

in the servlet, this is the content put in Header ..
 {"result":[{"A":"The first"},{"B":"The second"}]}

this is code the doPost function in the servlet ::
    public void doPost(HttpServletRequest request,
                       
HttpServletResponse response) throws ServletException,
                                                           
IOException {
       
//String requestParameter = request.getParameter("type");
       
        response
.setContentType(CONTENT_TYPE);
       
       
String arrayToJson = "";
       
        arrayToJson
= "{\"result\":[" +
           
"{\"A\":\"The first\"}," +
           
"{\"B\":\"The second\"}" +
       
"]}";
       
        response
.addHeader("result", arrayToJson);
   
}

How to connect and show the data, or how to send and receive data between them..

IDE: J Developer
Desktop OS: Windows 7

My regards,,
Thanks ^_^

Shai Almog

unread,
Jul 10, 2016, 11:48:45 PM7/10/16
to CodenameOne Discussions, samer.als...@gmail.com
Hi,
you are adding the result to a header in the servlet which doesn't make sense.
You need to write it to the response ideally with the right mime type see: http://www.oracle.com/technetwork/java/servlet-142430.html

I would also suggest starting with doGet as you can debug that in the browser and see that you get the right response.

On the Codename One side you need to use a regular ConnectionRequest. MultipartRequest is for file upload which is something very different.
Notice that the URL you gave is for your localhost and will not work on the device...

Something like this should work, notice that you don't need to catch an exception as those are asynchronous:

ConnectionRequest req = new ConnectionRequest("http://127.0.0.1:7101/TestServlet-SerlvletTest-context-root/servlet1") {
     
protected void readResponse(InputStream input) throws IOException  {
         
JSONParser p = new JSONParser();
         
Map<String, Object> parsedData = p.parseJSON(new InputStreamReader(input, "UTF-8"));
         
... do something with the JSON
     
}
   
protected void postResponse() {
       
... change the UI based on the results of readResponse
   
}
};
req
.setPost(true);
NetworkManager.getInstance().addToQueue(req);


samer.als...@gmail.com

unread,
Jul 12, 2016, 12:23:29 PM7/12/16
to CodenameOne Discussions, samer.als...@gmail.com
Thanks for your replay, it is good :)
I edit my codes and the app run correctly

the doPost function in the Servlet ::
I get the username and put it in string and return to user by response object ::
    public void doPost(HttpServletRequest request,
                       
HttpServletResponse response) throws ServletException,
                                                           
IOException {

        response
.setContentType(CONTENT_TYPE);

       
String username = request.getParameter("username");
       
       
String json = "";

        json
= "{\"result\":[" + "{\"username\":\"" + username + "\"}]}";
       
        response
.setContentType("application/json; charset=utf-8");
        response
.setCharacterEncoding("UTF-8");
        response
.getWriter().write(new String(json.getBytes("utf-8"), "utf-8"));
   
}

and the code of button event in the project ::
I get username from text field (username) and put the response from the Servlet in label (result) ::
 @Override
   
protected void onMain_ButtonAction(Component c, ActionEvent event) {

       
       
ConnectionRequest req = new ConnectionRequest("http://127.0.0.1:7101/TestServlet-SerlvletTest-context-root/servlet1") {

             
protected void readResponse(InputStream input) throws IOException  {
                 
JSONParser p = new JSONParser();
                 
Map<String, Object> parsedData = p.parseJSON(new InputStreamReader(input, "UTF-8"));

                 result
= parsedData.toString();
             
}
             
           
protected void postResponse() {
               
//... change the UI based on the results of readResponse
                findResult
().setText(result);
           
}
       
};
       
        req
.setPost(true);
        req
.setDuplicateSupported(true);
       
        req
.addArgument("username", findUsername().getText());
       
        req
.setTimeout(50000);
       
       
NetworkManager.getInstance().addToQueueAndWait(req);
   
}



Reply all
Reply to author
Forward
0 new messages