How android uploads images to PHP

Source: Internet
Author: User
The process of uploading images from android to PHP is described in detail ,. The process of uploading images from android to PHP is explained in detail. when I upload an avatar today, I always submit a connection timeout error. the error message is as follows: XXXXXXSokcetTimeOutXXXXXXXX and then I set the process of uploading images from android to PHP,

When uploading an avatar today, the connection timeout error is always submitted. the error message is as follows: XXXXXXSokcetTimeOutXXXXXXXX

Then set the HTTP timeout:
The code is as follows:
[Java] view plaincopyprint?
// Set the timeout value
Httpclient. setTimeout (20000 );

Building, runing, or not .... This is a strange thing. Obviously, how can it suddenly become a connection timeout! After a while, I also communicated with my friends at the backend. he also tested the upload interface and found that there was no problem, so I had to go over it myself ....
I am so depressed that I can't see any errors in the original code, and I can't do anything. I just want to make the best decisions. I need to set up a PHP image upload interface myself, test in person what is going on ....

1. first, you have to go to a convenient and fast PHP Server. here I use WampServer, Baidu ---- download ----- install ----- start, and the browser input: http: // 127.0.0.1 has a page, OK. That's easy!

2. enter http: // enter the local IP address and press Enter. an error is returned, similar to "You don't have permission to access/on this server", indicating that your WM has not been set, the following settings are required:
The cause of this problem is that the default configuration in Apache http. conf is
The code is as follows:
# Onlineoffline tag-don't remove
Order Deny, Allow
Deny from all
Allow from 127.0.0.1

Only access to 127.0.0.1 is allowed. click the wampserver icon and click Putonline. the default configuration in http. conf is automatically changed
The code is as follows:
# Onlineoffline tag-don't remove
Order Allow, Deny
Allow from all

Now localhost is accessible.

Similarly, phpMyadmin cannot be accessed normally under localhost and can be accessed normally at 127.0.0.1. solution:
Click the alias directory under the root directory to open the phpmyadmin. conf configuration file.
The code is as follows:
Deny from all
Allow from 127.0.0.1
Change
Allow from all

3. enter http: // enter the local IP address and press enter to display the page. OK! As for the second and third steps, I won't talk about them anymore... Leave it to new people to think about it! A great God directly ignores .....
4. write a PHP file for uploading images. of course, how can a child who knocks on java suddenly suppress it? what should I do? of course Baidu references others? the following PHP code comes from the network, no error during the test:

[Php] view plaincopyprint? <? Php $ base_path = "./upload/"; // store the 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 was an error uploading the file, pleas E try again! ". $ _ FILES ['Attach '] ['error']); echo json_encode ($ array) ;}?>

5. put the above php file in the www directory under the WM installation directory, as shown in my example, for reference only:

6. after the above steps, the PHP end has been set up, and now it is OK to go back to the android end and change the IP address. the code segment is as follows:

[Java] view plaincopyprint? // HTTP upload image RequestParams params = new RequestParams (); try {// Save the compressed bitmap as the image file String saveImgPath = getSD_Path () + "/saveimg.png "; file saveimg = new File (saveImgPath); FileOutputStream fos = new FileOutputStream (saveimg); bmp. compress (Bitmap. compressFormat. PNG, 100, fos); fos. flush (); fos. close (); // upload the compressed File, about KB. File uploadImg = new File (saveImgPath); params. put ("attach", uploadImg);} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} // Upload address String 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 successful. set the image face. setImageBitmap (bmp); ToastUtils. show (context, "uploaded successfully");} 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); the attach parameter here is one-to-one correspondence with the server. do not change it ....
String url = "http: // 192.168.0.8/upload. php"; 192.168.0.8 is the address of my PHP deployment. change it to your own address.


PS: Do not commit 2. if you use 127.0.0.1, you cannot use 127.0.0.1.

This is building and runing. Found OK .... You can upload the file and find the upload directory under the www Directory. the upload directory contains uploaded images ....


7. that's why you are wondering .... I again took the courage to find the PHP backend. after a heated discussion with him, I found that the server had crashed! 800 servers for one year ..... Alas... No more ....

Failed. when I upload my avatar today, I always submit a connection timeout error. the error message is as follows: XXXXXXSokcetTimeOutXXXXXXXX and set it myself...

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.