Android upload images to the entire process of PHP background

Source: Internet
Author: User
Tags php server

PS: Cheap server but will be the occasional pit you.

Today you are modifying some of the app's interactions and refactoring your code. Everything is so smooth, PA pa, runing, Test no problem, and then snapped ...

Suddenly, when the test upload avatar, always connect timeout .... The following error is reported:

Xxxxxxsokcettimeoutxxxxxxxx

Then set the HTTP timeout time yourself:

  Set timeout time  httpclient.settimeout (20000);

Again building,runing, still can't .... This is strange, obviously good, how can suddenly become the connection timeout! And after a while, but also with the backstage over the friend, he also tested the upload interface, found no problem, let me go to toss it ....

I am depressed, can not see the original code there are any errors, there is no way, on the worst way, I put a PHP upload image interface, personally test what is going on ....


1. First, you have to next convenient PHP server, I use wampserverhere, baidu----Download-----Installation-----Start, browser input: http://127.0.0.1 have page display, OK. It's so easy!

2. Browser input: HTTP//Native IP address Enter, found an error, similar to the "You do not have permission to access/on this server" description of your WM has not been set, need to do the following settings:

The cause of this problem is that the default configuration within Apache's http.conf is
# onlineoffline Tag-don ' t remove
Order Deny,allow
Deny from all
Allow from 127.0.0.1
Only allow 127.0.0.1 access, click the Wampserver icon and then click on the putonline,http.conf in the above default configuration to automatically change to
# onlineoffline Tag-don ' t remove
Order Allow,deny
Allow from all
Now localhost can access it.

Similarly phpmyadmin under localhost can not access normal access in 127.0.0.1, workaround:
Click the alias directory under the root directory, open the phpmyadmin.conf configuration file, and modify the http.conf as above
Deny from all
Allow from 127.0.0.1
Revision changed to
Allow from all


3. Re-enter:/HTTP//native IP address return display page ok! As for why the second step, the third step, I will not say ... Leave it to new people to think about it! The great God directly ignores ...

4. Write a php file to upload a picture, of course, I a knock on the Java children suddenly how can suppress the out, how to do, of course, Baidu reference others, the following PHP code from the network, pro-Test no error:

<?php$base_path = "./upload/"; Store directory if (!is_dir ($base _path)) {    mkdir ($base _path,0777,true);} $target _path = $base _path.  basename ($_files [' Attach '] [' name ']), if (Move_uploaded_file ($_files [' Attach '] [' tmp_name '], $target _path)) {$array = Array ("status" = = True, "msg" = = $_files [' Attach '] [' name ']); Echo Json_encode ($array);} else {$array = array ("status" = False, "msg" = "=" There is an error uploading the file, please try again! ". $_file S [' Attach '] [' ERROR ']; Echo Json_encode ($array);}? >

5. Put the above PHP file in the WM installation directory under the WWW directory, my as shown, for reference only:




6. After the above steps, the PHP side has been set up, now is back to the Android to change the IP address test is OK, the code snippet is as follows:

       HTTP upload image Requestparams params = new requestparams (); try {//Save compressed bitmap as Picture file string Saveimgpath=getsd_path () + "/sav Eimg.png "; File Saveimg=new file (Saveimgpath); FileOutputStream fos = new FileOutputStream (saveimg); bmp.compress (Bitmap.CompressFormat.PNG, N, Fos); Fos.flush (); Fos.close ();//upload compressed files, about 100k or so file uploadimg=new-file (Saveimgpath); <span style= "color: #ff0000;" >params.put ("Attach", uploadimg); </span>} catch (FileNotFoundException e) {e.printstacktrace ();} catch ( IOException e) {e.printstacktrace ();} Upload Address//string url=urlconfigs.uploadheadimage_ukey+myprefs. Ukey (). get (); <span style= "color: #ff0000;" >string url= "http://192.168.0.8/upload.php"; &LT;/SPAN&GT;//LOGUTIL.E (TAG, "Upload img URL:" +url); Asynchttputil.post_loading (Context,url, params, new Mytexthttpresponsehandler () {@Overridepublic void onsuccess (int Status, header[] arg1, String json) {super.onsuccess (status, Arg1, JSON); LOGUTIL.E (TAG, "Upload image json:" +json); Respondbaseentity Entity=gsonutil.getfroMjson (JSON, respondbaseentity.class); if (Entity.isstatus ()) {//upload succeeded, set picture Face.setimagebitmap (BMP); Toastutils.show (Context, "upload success");} Else{toastutils.show (context, JSON);} Myprefs.position (). put (0);} @Overridepublic void onfailure (int arg0, header[] arg1, String arg2, Throwable arg3) {super.onfailure (arg0, arg1, arg2, AR G3); Myprefs.position (). put (0);//arg3.printstacktrace (); Toastutils.show (context, r.string.network_unavailable);}

Params.put ("Attach", uploadimg); Here the attach parameter is corresponding to the service side one by one, do not change ....

String url= "http://192.168.0.8/upload.php"; This 192.168.0.8 is my PHP deployment address, changed to your own on the line.


PS: Don't make 2, use 127.0.0.1 think about why can't use 127.0.0.1

This is the building,runing.   Find OK .... Can upload, and found in the WWW directory upload directory, upload directory has uploaded pictures ....




7. It's a wonder ....  I summon up the courage to find a PHP back end, with his heated discussion, found that the server pits the father Ah! 800 blocks a year of servers ..... Alas... Do not say....



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android upload images to the entire process of PHP background

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.