How do I upload both form data and multiple files from my android app, using HTTPURLConnection to my server, where I am using PHP? -


i have android application sending 1 or more zip files server. i'd send additional form data along files, because form data used determine disposition of files.

i've been able upload files using multi-part content-type, can't seem php on server side see both form data , files.

what i'd know how send form data , list of files server using httpurlconnection on android side , php on server side.

i believe problem related content-type , content-disposition. i'd appreciate if point me @ definitive example, or explain i'm doing wrong.

thanks taking time @ problem.'

here's latest, revised android code:

string postdriverupload(file[] filelist) {         httpurlconnection connection = null;         dataoutputstream outputstream = null;         string tag = tag + "postdriverinfo()";         url url = null;         string result = ok_to_continue;          string targeturl = server + "/upload.php";          try {             url = new url(targeturl);         } catch (malformedurlexception e) {             result = "error: [" + targeturl + "] can't parsed url.";             log.e(tag, result, e);             return result;         }          try {             connection = (httpurlconnection) url.openconnection();         } catch (ioexception e) {             result = "error: ioexception.";             log.e(tag, result, e);             return result;         }          // allow inputs , outputs         connection.setdoinput(true);         connection.setdooutput(true);         connection.setusecaches(false);          // enable post method          try {             connection.setrequestmethod("post");         } catch (protocolexception e) {             result = "error: protocol exception.";             log.e(tag, result, e);             return result;         }          connection.setrequestproperty("connection", "keep-alive");         connection.setrequestproperty("content-type",                 "multipart/form-data;boundary=" + boundary);          // encode driver username         list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>();         try {             namevaluepairs.add(new basicnamevaluepair("username", driver                     .getusername()));             log.d(tag, "encoded username='driver' is: "                     + encodedata(namevaluepairs));              // create output stream send data server             outputstream = new dataoutputstream(connection.getoutputstream());             writeboundary(outputstream);             outputstream                     .writebytes("content-disposition: form-data; name=\"username\" "                             + eol + eol);             outputstream.writebytes(driver.getusername() + eol);             writeboundary(outputstream);              // starting files part             outputstream                     .writebytes("content-disposition: form-data; name=\"files\" "                             + eol + eol);             outputstream.writebytes("content-type: multipart/mixed; boundary="                     + file_boundary + eol);             // adding file parts             (file file : filelist) {                 writefileboundary(outputstream);                 // write out file                 string contenttype = string.format(                         "content-disposition:file; filename=\"%s\"" + eol                                 + "content-type: application/x-zip-compressed",                         file.getname())                         + eol + eol;                 log.d(tag, "content-type = " + contenttype);                 outputstream.write(contenttype.getbytes());             }             writefileboundary(outputstream);             writeboundary(outputstream);             outputstream.flush();             outputstream.close();         } catch (unsupportedencodingexception e) {             result = "error: unsupportedencodingexception ";             log.e(tag, result, e);             return result;         } catch (ioexception e) {             result = "error: ioexception ";             log.e(tag, result, e);             return result;         }          // response server (code , message)         try {             int serverresponsecode = connection.getresponsecode();             string serverresponsemessage = connection.getresponsemessage();             log.d(tag, "response code = " + serverresponsecode);             log.d(tag, "response message = " + serverresponsemessage);         } catch (ioexception e) {             result = "error getting response server.";             log.e(tag, result, e);             return result;         }         final stringbuilder out = new stringbuilder();         try {             bufferedreader in = new bufferedreader(new inputstreamreader(                     connection.getinputstream()));             string line;             while (null != (line = in.readline())) {                 if (line.contains("error")) {                     log.d(tag, "httpresponse: " + out.tostring());                     result = "error: httpresponse: " + out.tostring();                     break;                 }                 out.append(line + "\n\n");             }             log.d(tag, "httpresponse: " + out.tostring());          } catch (ioexception e) {             log.w(tag, "warning: ", e);         }          return result;     }   void writeboundary(dataoutputstream out) throws ioexception {     final string twohyphens = "--";     out.writebytes(twohyphens + boundary + eol); }  void writefileboundary(dataoutputstream out) throws ioexception {     final string twohyphens = "--";     out.writebytes(twohyphens + file_boundary + eol); } 

here's output of logging on android side. can see, key, username , value, rbenjamin, being loaded $_post variable, isn't right past point. delimit next section boundary, seems gobbled value variable 'files'. i'm close, i'm still missing something. (i stripped out of noise in log file below.)

: upload button clicked. : compiling upload file : getting delivered sql = select * deliveryorder delivereddatetime != '' : uploadenabled = true : downloadenabled = false : driveremail = ray.benjamin@gmail.com : starting, getting database. : found 1 drivers. : processing driver 1 : user name rbenjamin : email ray.benjamin@gmail.com : uploading files using http http://192.168.1.17:8080 : there 16 files. : checking download - enabled? false : encoded username='driver' is: username=rbenjamin : content-type = content-disposition:file; filename="upload20130820_1015.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130820_1048.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1624.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1628.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1645.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1647.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1705.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1709.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1746.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1801.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1804.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1811.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1813.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_1821.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130821_2050.zip"  : content-type: application/x-zip-compressed  :   : content-type = content-disposition:file; filename="upload20130825_1957.zip"  : content-type: application/x-zip-compressed  :   : response code = 200 : response message = ok : httpresponse: <!doctype html> : <html> : <body> : post defined.<br>key = username"_, value = rbenjamin<br>key = files"_, value = content-type: multipart/mixed; boundary=**file*boundary***25wlqf@*** : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130820_1015.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130820_1048.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1624.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1628.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1645.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1647.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1705.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1709.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1746.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1801.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1804.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1811.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1813.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_1821.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130821_2050.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@*** : content-disposition:file; filename="upload20130825_1957.zip" : content-type: application/x-zip-compressed : --**file*boundary***25wlqf@***<br> : notice: undefined index: username in /var/www-pbs/upload.php on line 15 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 : user name is: <br>files defined.<br> : notice: undefined index: file in /var/www-pbs/upload.php on line 24 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 : <p>dumping _files</p> : notice: undefined index: file in /var/www-pbs/upload.php on line 34 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 : notice: undefined index: file in /var/www-pbs/upload.php on line 40 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 : upload: <br> : notice: undefined index: file in /var/www-pbs/upload.php on line 41 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 : type  : <br> : notice: undefined index: file in /var/www-pbs/upload.php on line 42 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 : size  : 0 kb<br> : notice: undefined index: file in /var/www-pbs/upload.php on line 43 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 : stored in:  : notice: undefined index: file in /var/www-pbs/upload.php on line 45 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 : notice: undefined index: file in /var/www-pbs/upload.php on line 47 : call stack: :     0.0083     638888   1. {main}() /var/www-pbs/upload.php:0 :  exists. : </body> : </html> : 200 

no 1 answered question, found needed here: a www.codejava.net article on sending multipart forms programatically . doctor ordered. article provides class, multipartutility, makes easy upload using httpurlconnection. not answer, it's structured one. had tweak bit android, not much.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -