Android client uses okhttp to upload files to PHP server

Source: Internet
Author: User
Tags php server
in order to better in response to network access, recently learned a bit okhttp, feel very convenient to use, first to use okhttp, need to import Okhttp-x.x.x.jar in the project Okhttputils-x_x_x.jar Okio-x.x.x.jar These three jar packages, the following is the Android side and server-side key code:

private void Uplodeimage (file mfile) {//mfile A real existing picture file if (!mfile.exists ())        {            Toast.maketext ( Mainactivity.this, "file does not exist, please modify file path", Toast.length_short). Show ();            return;        } Okhttputils.post (). URL (uplode_url).    addfile ("File", Mfile.getname (), Mfile)    . Build ()    . Execute (NEW Callback
 
  
   
  () {@Overridepublic void OnError (call arg0, Exception arg1) {System.out.println ("failed to upload file"); Info.settext ("Reeor:upload error!");} @Overridepublic void Onresponse (String arg0) {//system.out.println (arg0);//upload successfully returns the unique path of the file on the server, Save this path to the database Info.settext (arg0);//This will return a unique path containing the uploaded file on the server, save the string to the database after the upload is successful} @Overridepublic string Parsenetworkresponse (Response arg0) throws Exception {//TODO auto-generated method Stubreturn Arg0.body (). String ();}}) ;}
 
  

Server-side code:

 Filename= $fileName, $this->maxsize= $maxSize, $this->allowmime= $allowMime; $this->allowext= $allowExt; $ This->uploadpath= $uploadPath, $this->imgflag= $imgFlag $this->fileinfo=$_files[$this->filename];} /** * Detect error Uploading File * @return Boolean */protected function CheckError () {if (!is_null ($this->fileinfo)) {if ($this, fileinfo[' ERROR ']>0) {switch ($this->fileinfo[' error ') {Case 1: $this->error= ' exceeded the PHP config file upload_max_ The value of the FileSize option '; Break;case 2: $this->error= ' exceeds the value max_file_size set in the form '; Break;case 3: $this->error= ' file is partially uploaded '; Break;case 4: $this->error= ' no option to upload files '; Break;case 6: $this->error= ' no temporary directory found '; Break;case 7: $this->error= ' File not writable '; Break;case 8: $this->error= ' due to PHP extension interrupt file Upload '; return false;} Else{return true;}} else{$this->error= ' file error '; return false;}} /** * Detects the size of the uploaded file * @return boolean */protected function Checksize () {if ($this->fileinfo[' size ']> $this->maxsize) {$this->error= ' upload file too large '; return false;} return true;} /** * Detect Extension * @return Boolean */protected function Checkext () {$this->ext=strtolower (pathinfo ($this->fileinfo[' name '],pathinfo_extension)); In_array ($this->ext, $this->allowext)) {$this->error= ' disallowed extension '; return false;} return true;} /** * Detection File Type * @return Boolean */protected function Checkmime () {if (!in_array ($this->fileinfo[' type '], $this- Allowmime) {$this->error= ' disallowed file type '; return false;} return true;} /** * Detects if it is a real picture * @return Boolean */protected function checktrueimg () {if ($this->imgflag) {if (! @getimagesize ($this- >fileinfo[' Tmp_name ')) {$this->error= ' is not a real picture '; return false;} return true;}} /** * detects if an HTTP post is uploaded to the * @return Boolean */protected function Checkhttppost () {if (!is_uploaded_file ($this- fileinfo[' Tmp_name ') {$this->error= ' file is not uploaded via HTTP post '; return false;} return true;} /** * Display Error */protected function ShowError () {return (' Rtype ' =>-30, ' rmessage ' = $this->error));} /** * Detect directory does not exist create */protected function Checkuploadpath () {if (!file_exists ($this->uploadPath) {mkdir ($this->uploadpath,0777,true);}} /** * Generates a unique string * @return string */protected function Getuniname () {return MD5 (Uniqid (Microtime (True), true));} /** * Upload file * @return string */public function UploadFile () {if ($this->checkerror () && $this->checksize () && $this->checkext () && $this->checkmime () && $this->checktrueimg () &&$ This->checkhttppost ()) {$this->checkuploadpath (); $this->uniname= $this->getuniname (); $this destination= $this->uploadpath. ' /'. $this->uniname. '. $this->ext;if (@move_uploaded_file ($this->fileinfo[' tmp_name '), $this->destination) {return Array (' Rtype ' + 0, ' rmessage ' = $this->destination);} else{$this->error= ' file move failed '; $this->showerror ();}} Else{return $this->showerror ();}}}

 
  UploadFile (), Json_unescaped_unicode);


The above describes the Android client using okhttp upload file PHP server, including the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.