The entire process of uploading images to the PHP background from android
PS: cheap servers may be difficult for you.
Today, I am modifying some app interactions and restructuring code. Everything went so smoothly. No problem in testing, no problem...
All of a sudden, connection times out while testing the upload of an avatar .... The following error is reported:
XXXXXXSokcetTimeOutXXXXXXXX
Then set the HTTP Timeout:
// 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
# 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
# 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.
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:
True, msg => $ _ FILES ['Attach '] ['name']); echo json_encode ($ array);} else {$ array = array (status => false, msg => There was an error uploading the file, please 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:
// 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 () {@ 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 successful. Set the image face. setImageBitmap (bmp); ToastUtils. show (context, uploaded successfully);} 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, 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 ....