Android upload picture to PHP process detailed _php Example

Source: Internet
Author: User
Tags json php server phpmyadmin

Today in doing upload avatar, always submit the connection Timeout error, the errors are as follows: Xxxxxxsokcettimeoutxxxxxxxx

Then set the HTTP timeout yourself:

Copy Code code as follows:

[Java] View plaincopyprint?
Set timeout time
Httpclient.settimeout (20000);

Again building,runing, still can't .... This is strange, obviously good, how can suddenly become connection timeout! And toss for a while, but also with the backstage side of the friends communicated, he also tested upload interface, found no problem, let me go to toss it ....
I am depressed, I do not see the original code has any errors, there is no way, on the most unwise it, their own build a PHP upload image interface, personally test what is going on in the end ....

1. First, you have to next a convenient and efficient PHP server, I used the wampserver, Baidu----Download-----Installation-----Start, browser input: http://127.0.0.1 has a page, OK. It's that simple!

2. Browser input: http://native IP address return, found an error, similar to the "You don ' t have permission to access/on this server" shows that your wm has not been set up, you need to do the following settings:
The reason for this problem is that the default configuration within the Apache http.conf is

Copy 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 on the Wampserver icon to putonline,http.conf the above default configuration automatically changed to
Copy Code code as follows:

# onlineoffline Tag-don ' t remove
Order Allow,deny
Allow from all

Now the localhost can be accessed.

Similarly phpmyadmin under localhost can not normally access the normal access in 127.0.0.1, the solution:
Click the alias directory under the root directory, open the phpmyadmin.conf configuration file, and the above modified http.conf the same

Copy Code code as follows:

Deny from all
Allow from 127.0.0.1
Amended to
Allow from all

3. Then 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 the newlyweds to think about it! The great God directly ignores ...
4. Write a upload picture of the php file, of course, I a knock on the Java child how can be suppressed, then how to do, of course, Baidu reference to others, the following PHP code from the network, the test is not wrong:

[PHP] View plaincopyprint?
<?php 
$base _path = "./upload/";//Storage 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" => c10/> "MSG" => $_files [' Attach '] [' name ']  
  ); 
  echo Json_encode ($array); 
} else { 
  $array = array ( 
      "status" => false, 
      "MSG" => "There was a error uploading the file, please tr Y again! ". $_files [' Attach '] [' ERROR ']]  
  ; 
  echo Json_encode ($array); 
> 

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

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

[Java] View plaincopyprint? 
HTTP upload picture Requestparams params = new Requestparams (); 
  try {//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, FOS); 
  Fos.flush (); 
  Fos.close (); 
  Upload compressed files, about 100k 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";</span> logutil.e (TAG, "Upload img URL:" +url); Asynchttputil.post_loading (Context,url, params, new Mytexthttpresponsehandler () {@Override public void onsuccess (in T status, header[] arg1, String JSON) {sUper.onsuccess (status, Arg1, JSON); 
    LOGUTIL.E (TAG, "Upload picture json:" +json); 
    Respondbaseentity Entity=gsonutil.getfromjson (JSON, respondbaseentity.class); 
      if (Entity.isstatus ()) {//uploaded successfully, 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 (a 
    Rg0, 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 with the service end one by one, do not change ....
String url= "http://192.168.0.8/upload.php"; This 192.168.0.8 is the address of my PHP deployment, and you can change it to your own.


PS: Do not make 2, use the 127.0.0.1 to think why can't use 127.0.0.1

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


7. It's a wonder ....  I also summon up the courage to find the PHP back end, with his heated discussion after some, found that the server pit Dad! 800 dollars a year of server ... Alas... Do not say....

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.