networking - Working code for uploading mp3 to server on android -


i have searched on code uploading mp3 or similar file php server , have tested on 10 samples none has been successful far. codes have checked out either full of bugs or use outdated libraries.

i appreciate if has working code sample. possibly 1 uses volley or similar library. appreciate or code points me in right direction.

thanks

you can use loopj android asynchronous http client lib uploading file php server. download lib file given link , put project's libs folder , use code uploading file.

public void postfile(){     requestparams params = new requestparams();     params.put("filetitle","myfile1");     params.put("file", new file("file path here")); // e.g environment.getexternalstoragedirectory().getpath() + "/test.mp3"      asynchttpclient client = new asynchttpclient();      client.post("http://www.yourweserviceurlhere.com", params, new asynchttpresponsehandler() {         @override         public void onsuccess(int statuscode, header[] headers, byte[] responsebody) {             // todo auto-generated method stub          }          @override         public void onfailure(int statuscode, header[] headers,                 byte[] responsebody, throwable error) {             // todo auto-generated method stub          }                                                                                                                                                   });  } 

if want progress of uploading. can use custom design class. require common io 2.4 lib reference in converting httpresponse string.

public class asyncloader { private string url; private loadercallbackhandler mcallback; private context mcontext; private requestparams params; private requesthandle handle;  public interface loadercallbackhandler {             public void onstartuploading();     public void uploadcomplete(string response);     public void failedwitherror(throwable error);     public void progressupdate(long byteswritten, long bytestotal);     public void oncancle();     public void onfinish(); }   public asyncloader(context mcontext,string url,requestparams params, loadercallbackhandler callback) {     this.mcontext = mcontext;     this.url = url;     this.params = params;     this.mcallback = callback;       }  public void starttransfer() {            asynchconfig.mclient.settimeout(50000);             handle = asynchconfig.mclient.post(mcontext, url, params,handlerinterface); }     private responsehandlerinterface handlerinterface = new responsehandlerinterface() {      @override     public void sendstartmessage() {         if(mcallback != null) {             mcallback.onstartuploading();                        }      }      @override     public void sendresponsemessage(httpresponse response) throws ioexception {         httpentity entity = response.getentity();         if (entity != null) {             inputstream instream = entity.getcontent();              // todo convert in stream jsonobject , whatever need              stringwriter writer = new stringwriter();             ioutils.copy(instream, writer, charset.defaultcharset());             string thestring = writer.tostring();              if(mcallback != null) {                 mcallback.uploadcomplete(thestring);             }         }      }      @override     public void sendsuccessmessage(int arg0, org.apache.http.header[] arg1, byte[] arg2) {      }      @override     public void sendfailuremessage(int arg0, org.apache.http.header[] arg1,             byte[] arg2, throwable error) {             if(mcallback != null) {                  mcallback.failedwitherror(error);             }     }      @override     public void sendfinishmessage() {         if(mcallback != null) {             mcallback.onfinish();         }     }      @override     public void sendprogressmessage(long byteswritten, long bytestotal) {         if(mcallback != null) {                          mcallback.progressupdate(byteswritten, bytestotal);         }      }      @override     public void setusesynchronousmode(boolean arg0) {     }      @override     public void setrequesturi(uri arg0) {     }      @override     public void setrequestheaders(org.apache.http.header[] arg0) {     }      @override     public uri getrequesturi() {         return null;     }     @override     public org.apache.http.header[] getrequestheaders() {         return null;     }      @override     public void sendcancelmessage() {          if(mcallback != null) {             mcallback.oncancle();             mcallback.onfinish();         }     }      @override     public void sendretrymessage(int retryno) {         // todo auto-generated method stub      }      @override     public boolean getusesynchronousmode() {         // todo auto-generated method stub         return false;     }      @override     public void setusepoolthread(boolean usepoolthread) {         // todo auto-generated method stub      }      @override     public boolean getusepoolthread() {         // todo auto-generated method stub         return false;     }      @override     public void onpreprocessresponse(responsehandlerinterface instance,             httpresponse response) {         // todo auto-generated method stub      }      @override     public void onpostprocessresponse(responsehandlerinterface instance,             httpresponse response) {         // todo auto-generated method stub      }      @override     public void settag(object tag) {         // todo auto-generated method stub      }      @override     public object gettag() {         // todo auto-generated method stub         return null;     } };  /** * cancel upload calling method */ public void cancel() throws exception {     asynchconfig.mclient.cancelallrequests(true);     handle.cancel(true);  } } 

asynch config class

public final class asynchconfig {         public static asynchttpclient mclient = new asynchttpclient(); } 

use

requestparams params = new requestparams(); params.put("filetitle","myfile1"); params.put("file", new file("file path here")); // e.g environment.getexternalstoragedirectory().getpath() + "/test.mp3"  asyncloader asyncuploader = new asyncloader(this, "url_here", params, callhandler); asyncuploader.starttransfer(); 

callhandler interface object

loadercallbackhandler callhandler = new loadercallbackhandler() {      @override     public void uploadcomplete(string response) {         // todo auto-generated method stub      }      @override     public void progressupdate(long byteswritten, long bytestotal) {         // todo auto-generated method stub      }      @override     public void onstartuploading() {         // todo auto-generated method stub      }      @override     public void onfinish() {         // todo auto-generated method stub      }      @override     public void oncancle() {         // todo auto-generated method stub      }      @override     public void failedwitherror(throwable error) {         // todo auto-generated method stub      } }; 

php service handling uploaded file

if(isset($_files['file']) && isset($_post['filetitle']) ) { include './config.php';  //randomly genrate file name $stickertmp = explode(".", $_files["file"]["name"]); $file = md5(date("l, f d, y h:i" ,time()) . (microtime())).".".end($stickertmp);  //geting temp location of file $filetemploc=$_files['file']['tmp_name'];   //path uploading specific location $pathandname="file_store/".$file;  // moving file specified path $resultupload = move_uploaded_file($filetemploc, $pathandname);  // if file moved on specified path insert reference db if($resultupload == true) {     //echo "file has been moved : ". $filetemploc . "  :".$pathandname;      $qinsert = "insert file_lists values (null,'".$_post['filetitle']."','".$file."') ";     mysql_query($qinsert); }  } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -