Search code examples
javaimagepostlwuitcodenameone

How do I post a picture/image using the IO Codenameone


Because codenameone can not use external libraries (HttpConnection) then I have to use the internal library / API provided Codenameone, it's just that I've managed to post the data to format text / string by using ConnectionRequest, I want to know is there any way to post the data in the form of an image with using ConnectionRequest? Thank you for your help

Snippet ConnectionRequest i'm using:

ConnectionRequest myrequest = new ConnectionRequest();
                                        myrequest.setUrl("http://www.xxxx.com/mobile/login/");
                                        myrequest.setPost(true);
                                        myrequest.addArgument("email", "[email protected]");
                                        myrequest.addArgument("password", "xxx");
                                        myrequest.setPriority(ConnectionRequest.PRIORITY_CRITICAL);
                                        NetworkManager.getInstance().addToQueue(myrequest);
                                        myrequest.addResponseListener(new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent evt) {
                                NetworkEvent n = (NetworkEvent)evt;





                                // gets the data from the server as a byte array...
                                byte[] data = (byte[])n.getMetaData();
                                String response = new String(data);
                            }
                        });

Solution

  • Sure you can just add the image data as an argument to the request but you will need to encode it. Alternatively you can override the method:

    protected void buildRequestBody(OutputStream os) throws IOException
    

    And write into the post output stream any arbitrary data you need.