Android uploads files to PHP server

Source: Internet
Author: User
Tags php server

The first two months had the privilege of attending a free training program and developing a small piece of software. Found that asynchttpclient is really useful.

Straight to the theme, Android uploads files to the PHP server:

1.PHP Terminal Server:

<?php//link database include ("config/db.php");//Get user Id$userid = $_post[' userid '];//process upload file $base_path = "upload/"; $fileName =$_files[' file ' [' Name ']; $name =explode ('. ', $fileName);      $userpicads = $base _path.  ' User_ '. $userid. '. '. $name [1]; /* Return Status code: 300: Process Success 301: Server Exception */$status =301;if (Move_uploaded_file ($_files [' file '] [' tmp_name '], $userpicads)) {  $ status=300; } else {  $status = 301;} If the file is saved successfully, update the database if ($status ==300) {$sql = "update mh_user set userpicads= ' {$userpicads} ' where id={$userid}"; $pdo EXEC ($sql);} The output returns the result $ret = Array (' status ' = $status); echo Json_encode ($ret);? >

2. Android Add Network permissions

<uses-permission android:name= "Android.permission.INTERNET"/>

  

3. Android Import the following jar packages (which can be easily downloaded to):

Android-async-http-1.4.7.jar (required)
Gson-2.1.jar (optional, parsing JSON format)
Httpcore-4.4.4.jar (optional, with Android Stutio may also need to import this)

4. Android Main code:

int userid = 1; String username = "HelloWorld"; String filepath = "/mnt/sdcard/download/mm.jpg"; String uploadurl = "http://192.168.1.103/test/upload.php"; Btn_upload.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {asynchttpclient client = new Asynchttpclient (); Requestparams params = new Requestparams ();//Add Parameter Params.put ("userid", UserID);p arams.put ("username", username); try {/ /Add Files params.put ("File", new file (filepath));} catch (FileNotFoundException e) {e.printstacktrace ();} Client.post (Uploadurl, params, new Asynchttpresponsehandler () {@Overridepublic void onsuccess (int i, Org.apache.http.header[] headers, byte[] bytes) {try {//Get back the contents of string resp = new string (bytes, "utf-8");//Handle the returned content here, such as parsing json or something ...} catch (Unsupportedencodingexception e) {e.printstacktrace ();}} @Overridepublic void onfailure (int i, org.apache.http.header[] headers, byte[] bytes, throwable throwable) {// Handle connection failed processing here ...});}); 

  

This article mainly refer to:

http://blog.csdn.net/fancylovejava/article/details/13506745

How PHP renames the uploaded file:

Http://zhidao.baidu.com/link?url=lipsDeUjztDw-kwpcWcs7b16_GEer7T2r7wBM3MMzRpBCZuct-wikqv6j5vsZxK39KotlB8qH8bVXOqRCXHosq

Android Studio may not be able to find Org.apache.http.Header, refer to:

Http://zhidao.baidu.com/link?url=_ Rm6ndkff2fpuf6i-i97fiykpnwpshcg7egyfxjtjgahzlln8dlt55vx02sd3epoew72bwu2xbwqln_yfhxbgw0nkxqbctmzsbo51upepri

Android uploads files to PHP server

Related Article

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.