How android uploads images to PHP _ php instance

Source: Internet
Author: User
Tags php server
This article describes how to upload images from android to PHP. For more information, see the connection timeout error message: 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 ....

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.