Android upload images to PHP in the process of detailed, _php tutorial

Source: Internet
Author: User

Android upload image to PHP in the process of detailed,


Today, when uploading the avatar, always submit a connection timeout error, the error message is as follows: Xxxxxxsokcettimeoutxxxxxxxx

Then set the HTTP timeout time yourself:
Copy the Code code as follows:
[Java] View plaincopyprint?
Setting the time-out period
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 wampserver here, 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
Copy the Code code as follows:
# 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
Copy the Code code as follows:
# 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
Copy the Code code as follows:
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] View plaincopyprint?<?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,< c3/> "msg" = = $_files [' Attach '] [' name ']    );   echo Json_encode ($array);  } else {   $array = array (       "status" = False,       "msg" = "=" There is an error uploading the file, please try Again! ". $_files [' Attach '] [' ERROR ']    ;   

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:

[Java] View plaincopyprint? HTTP upload image Requestparams params = new Requestparams ();   try {//To save the compressed bitmap as a picture file String Saveimgpath=getsd_path () + "/saveimg.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);params.put ("Attach", uploadimg);} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}//Upload address S Tring Url=urlconfigs.uploadheadimage_ukey+myprefs. Ukey (). get ();String url= "http://192.168.0.8/upload.php";     LOGUTIL.E (TAG, "Upload img URL:" +url); Asynchttputil.post_loading (Context,url, params, new Mytexthttpresponsehandler () {@Override public 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);  } @Override public void onfailure (int arg0, header[] arg1, String arg2, Throwable arg3) {super.onfailure (arg0,     Arg1, arg2, ARG3);         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....

http://www.bkjia.com/PHPjc/1042684.html www.bkjia.com true http://www.bkjia.com/PHPjc/1042684.html techarticle Android upload pictures to the PHP process in detail, today in the upload avatar, always submit the connection timeout error, the error message is as follows: Xxxxxxsokcettimeoutxxxxxxxx then set up ...

  • 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.