Uploading image file and string to php backend using android and post method -


i having trouble uploading image file along string using post method. image file in fact upload server, attempt send string (username) has failed. know getting value username because show's username in log when debug it. idea's why not working. here doinbackground

protected string doinbackground(string... arg0) {

    try {         string thepic = (string) arg0[0];         string name = (string) arg0[1];         string sourcefileuri = thepic;          log.d("thename",name);         log.d("pic", thepic);         httpurlconnection conn = null;         dataoutputstream dos = null;         string lineend = "\r\n";         string twohyphens = "--";         string boundary = "*****";         int bytesread, bytesavailable, buffersize;         byte[] buffer;         int maxbuffersize = 1 * 1024 * 1024;         file sourcefile = new file(sourcefileuri);          if (sourcefile.isfile())         {             log.d("checkfile", "its file");             try {                 string uploadserveruri = "http://*myaddress*.com/upload.php";                  // open url connection servlet                 fileinputstream fileinputstream = new fileinputstream(sourcefile);                 url url = new url(uploadserveruri);                  // open http connection url                 conn = (httpurlconnection) url.openconnection();                 conn.setdoinput(true); // allow inputs                 conn.setdooutput(true); // allow outputs                 conn.setusecaches(false); // don't use cached copy                 conn.setrequestmethod("post");                 conn.setrequestproperty("connection", "keep-alive");                 conn.setrequestproperty("enctype","multipart/form-data");                 conn.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary);                 conn.setrequestproperty("filetoupload", sourcefileuri);                 conn.setrequestproperty("uname", name);                   dos = new dataoutputstream(conn.getoutputstream());                  dos.writebytes(twohyphens + boundary + lineend);                 dos.writebytes("content-disposition: form-data; name=\"filetoupload\";filename=\"" + sourcefileuri + "\"" + lineend);                 dos.writebytes("content-disposition: form-data; name=\"uname\"" + lineend);                  dos.writebytes(lineend);                 string s = integer.tostring(dos.size());                 log.d("dosfilesize", s);                  // create buffer of maximum size                 bytesavailable = fileinputstream.available();                  buffersize = math.min(bytesavailable, maxbuffersize);                 buffer = new byte[buffersize];                  // read file , write form...                 bytesread = fileinputstream.read(buffer, 0, buffersize);                  while (bytesread > 0)                 {                      dos.write(buffer, 0, buffersize);                     bytesavailable = fileinputstream.available();                     buffersize = math.min(bytesavailable, maxbuffersize);                     bytesread = fileinputstream.read(buffer, 0,buffersize);                  }                  // send multipart form data necesssary after file                 // data...                 dos.writebytes(lineend);                 dos.writebytes(twohyphens + boundary + twohyphens + lineend);                  // responses server (code , message)                 serverresponsecode = conn.getresponsecode();                 string serverresponsemessage = conn.getresponsemessage();                  if (serverresponsecode == 200)                 {                     log.d("server response",serverresponsemessage);                      // messagetext.settext(serverresponsemessage);                     //toast.maketext(this, "file upload complete.",toast.length_short).show();                              //recursivedelete(mdirectory1);                  }                  // close streams //                 fileinputstream.close();                 dos.flush();                 dos.close();              }             catch (exception e)             {                  //dialog.dismiss();                 e.printstacktrace();                           //messagetext.settext("got exception : see logcat ");                         //toast.maketext(uploadtoserver.this, "got exception : see logcat ",toast.length_short).show();                 log.e("upload file server exception", "exception : "+ e.getmessage(), e);             }             // dialog.dismiss();          } // end else block         else         {             log.d("no image", "source not iamge file");         }       } catch (exception ex) {         // dialog.dismiss();          ex.printstacktrace();     }     return "executed"; } 

and here's php code

<?php $myfile = fopen("userlog.txt", "a");     fwrite($myfile, "\n\n\n\r\r\n"); fwrite($myfile, "\n\n\n\r\r\n"); fwrite($myfile, date("f j, y, g:i a") . "\r"); fwrite($myfile, $_server['remote_addr']); fclose($myfile);  $target_dir = "uploads/"; $target_file = $target_dir . basename($_files["filetoupload"]["name"]); $uploadok = 1; $imagefiletype = pathinfo($target_file,pathinfo_extension); $uname = $_post["uname"]; $status1 = false; $status2 = false;     $check = getimagesize($_files["filetoupload"]["tmp_name"]);     if($check !== false) {         $uploadok = 1;         $con = $con = mysql_connect("localhost:3036","username","password");          if          (!$con)           {           die('could not connect: ' . mysql_error());           }         mysql_select_db("rate_it", $con);         $imgdata = file_get_contents($_files["filetoupload"]["tmp_name"]);         $zero = 0;         $pid = "null";         $name = "car789";         $date = "curdate()";         $sql = sprintf("insert picture         (pid, uname, image, views, ratetotal, uploadname, uploaddate)         values         ('%s', '%s', '%s', '%d', '%d' ,'%s' ,%s)",         $pid,         $uname,         mysql_real_escape_string($imgdata),         $zero,         $zero,         $target_file,         $date         );         $myfile = fopen("test.txt", "a");         fwrite($myfile, "\n\n\n\r\r\n");         fwrite($myfile, "\n\n\n\r\r\n");         fwrite($myfile, date("f j, y, g:i a"));         fwrite($myfile, "$sql\n");         fclose($myfile);         if ( mysql_query($sql) == true) {             $status1 = true;         }          else {             $status1 = false;         }         mysql_close($con);      } else {         $uploadok = 0;     } if (move_uploaded_file($_files["filetoupload"]["tmp_name"], $target_file)) {         $status2 = true;     } else {         $status2 = false;     } if($status1 == true && $status2 == true){     echo "true"; }    else{     echo "false"; }  ?> 

directly after

            dos = new dataoutputstream(conn.getoutputstream()); 

add following code:

            dos.writebytes(twohyphens + boundary + lineend);             dos.writebytes("content-disposition: form-data; name=\"uname\"" + lineend);             dos.writebytes(lineend);             dos.writebytes(name + lineend);              dos.writebytes(twohyphens + boundary + lineend);             dos.writebytes("content-disposition: form-data; name=\"thepic\"" + lineend);             dos.writebytes(lineend);             dos.writebytes(thepic + lineend); 

remove lines `uname in them did not work.

also grab page php script echos add following code @ end of try block:

inputstream in = new bufferedinputstream(conn.getinputstream());  bufferedreader reader = new bufferedreader(new inputstreamreader(in)); stringbuilder sb = new stringbuilder(); string newline = system.getproperty("line.separator"); string line; while ((line = reader.readline()) != null) {     sb.append(line + newline);   } string resultpage = sb.tostring(); 

looking @ php script not receive. "true" or "false";

`


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -