MultiPartRequest does nothing

62 views
Skip to first unread message

Bruno Neumann

unread,
Sep 30, 2014, 2:37:47 PM9/30/14
to codenameone...@googlegroups.com
IDE: NetBeans/Eclipse/IDEA
Desktop OS; Linux Ubuntu
Simulator: Iphone 3gs

Hi. 
I'm trying to send a simple file with MultipartRequest but get no response error, only my file is not sent. My method is similar to this thread: 
The settings from my apache / php are all right and I can send a file through another system in the same folder. 

The method:
        
        MultipartRequest req = new MultipartRequest();
        req.setUrl("http://localhost/client");
        req.setPost(true);
        InputStream is = null;
        String filename = "/home/bruno/test.txt";

        try {
            is = FileSystemStorage.getInstance().openInputStream(filename);
            req.addData("content", is, FileSystemStorage.getInstance().getLength(filename), "text/plain");
            req.setFilename("content", filename);
            NetworkManager.getInstance().addToQueue(req);
        } catch (IOException ioe) { 
            System.out.println("ERROR");
        }


Would like an opinion of you, to check if I'm not forgetting something. 
Thanks guys

Shai Almog

unread,
Sep 30, 2014, 11:52:47 PM9/30/14
to codenameone...@googlegroups.com
Hi,
the URL to the file is illegal for a mobile device so that will not work there.
Also filename should be relative in the setFilename method.

Bruno Neumann

unread,
Oct 1, 2014, 12:07:04 PM10/1/14
to codenameone...@googlegroups.com
Hi Shai. 
I actually forgot the correct path. 
I created the file in the correct place now where I can read by the simulator, but still is not sending the file.
The relation of the filename in setFilename is correct now, right?

     
        MultipartRequest req = new MultipartRequest();

        req
.setUrl("http://localhost:8080/client");
        req
.setPost(true);


       
String filename = "file://home//text.txt";


       
try {
            req
.addData("text", filename, "text/plain");
            req
.setFilename("text", filename);
           
NetworkManager.getInstance().addToQueue(req);
       
} catch (IOException ioe) {
            ioe
.printStackTrace();
       
}

Thank you for your attention.

Shai Almog

unread,
Oct 2, 2014, 12:14:01 AM10/2/14
to codenameone...@googlegroups.com
Hi,
is this on device or simulator?
The file will not be there in the device either.

Bruno Neumann

unread,
Oct 2, 2014, 7:17:15 AM10/2/14
to codenameone...@googlegroups.com
Hi, 
is on simulator. 
When I list the files in the home through the Storage.getInstance().listEntries(); I see the file there. 
How may I direct this file to be sent via http?
Thank you Shai.

Chen Fishbein

unread,
Oct 2, 2014, 8:40:46 AM10/2/14
to codenameone...@googlegroups.com
Hi,
You can try like this:

is = FileSystemStorage.getInstance().openInputStream(filename);
req.addData("source", is, FileSystemStorage.getInstance().getLength(filename), mime);

Just to make sure it's not a path thing and that you are able to open the file, other then that I'd recommend you to use the NetworkMonitor on the Simualtor and check what was sent to the server and check on the server what you are getting.

Bruno Neumann

unread,
Oct 2, 2014, 1:04:44 PM10/2/14
to codenameone...@googlegroups.com
Hi Chen!
I'm doing the same as you mentioned, and I am able to read and write to the destination.
Here is what the NetworkMonitor shows..


Is the right response not see anything?

Thanks

Shai Almog

unread,
Oct 2, 2014, 1:23:21 PM10/2/14
to codenameone...@googlegroups.com
Hi,
check this where we show the exact code for client/server file upload with response:
http://www.codenameone.com/blog/build-mobile-ios-apps-in-java-using-codename-one-on-youtube

Chen Fishbein

unread,
Oct 2, 2014, 10:16:03 PM10/2/14
to codenameone...@googlegroups.com
Hi,
Notice you are getting a 301 which means a redirect type of some sort, this might be your problem.

Bruno Neumann

unread,
Oct 3, 2014, 9:41:52 AM10/3/14
to codenameone...@googlegroups.com
You're right Chen, the reason the file is not being sent is because I am getting the response code 301. 
I downloaded the link Photoshare project recommended by Shai, only modified the url in the code, the rest I am running fully, and also get the 301 response code in command createPictureCommand. 
When I run a simple php script sending a file to the same place, I get the expected POST - 200. 
So I take it this is something I'll have to study more about it.
Thank you guys.
Reply all
Reply to author
Forward
0 new messages