Table of Contents
Detailed explanation of the process of uploading images to PHP in android,
Home Backend Development PHP Tutorial Detailed explanation of the process of uploading images to PHP from android, _PHP tutorial

Detailed explanation of the process of uploading images to PHP from android, _PHP tutorial

Jul 13, 2016 am 09:45 AM
android upload

Detailed explanation of the process of uploading images to PHP in android,

When uploading avatars today, a connection timeout error was always submitted. The error message is as follows: XXXXXXSokcetTimeOutXXXXXXXX

Then set the HTTP timeout yourself:
Copy code The code is as follows:
[java] view plaincopyprint?
//Set the timeout
httpclient.setTimeout(20000);

No matter how much building or running, it still doesn’t work. . . . This is strange. It's obviously working fine, but why does the connection suddenly become a timeout? After struggling for a while, I also communicated with my friend at the backend. He also tested the upload interface and found that there was no problem, so he let me go to work on it myself. . . .
I was depressed. I couldn't see any errors in the original code, and there was nothing I could do. So I decided to make a PHP image upload interface and test it myself. . . .

1. First, you need to download a convenient and fast PHP server. I used WampServer here. Baidu----Download-----Installation-----Start, browser input: http:// A page is displayed on 127.0.0.1, OK. It's that simple!

2. Browser input: http://local IP address and press Enter. An error message is found, similar to "You don't have permission to access / on this server". This means that your WM has not been set up yet and you need to make the following settings. :
The reason for this problem is that the default configuration in Apache's http.conf is
Copy code The code is as follows:
# onlineoffline tag - don't remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1

Only 127.0.0.1 is allowed to access. Click the wampserver icon and then click Putonline. The above default configuration in http.conf will be automatically modified to
Copy code The code is as follows:
# onlineoffline tag - don't remove
Order Allow,Deny
Allow from all

Now localhost can be accessed.

Similarly, phpMyadmin cannot be accessed normally under localhost but can be accessed normally under 127.0.0.1. Solution:
Click the alias directory under the root directory, open the phpmyadmin.conf configuration file, and change
as you did above to modify http.conf Copy code The code is as follows:
Deny from all
Allow from 127.0.0.1
Modify to
Allow from all

3. Enter again: http://local IP address and press Enter to display the page OK! As for why the second and third steps are needed, I won’t say anything. . . Let the newcomers think about it! The great god simply ignored it. . . . .
4. Write a PHP file for uploading pictures. Of course, I am a child who can type Java. How can I hold it in all of a sudden? What should I do? Of course, Baidu refers to others. The following PHP code comes from the Internet. There is no error in my personal test:

[php] view plaincopyprint?
<&#63;php 
$base_path = "./upload/"; //存放目录 
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, please try again!" . $_FILES ['attach'] ['error']  
  ); 
  echo json_encode ( $array ); 
} 
&#63;> 
Copy after login

5. Place the above php file in the www directory under the WM installation directory. Mine is as shown below, for reference only:

6. After the above steps, the PHP side has been set up. Now it is OK to go back to the android side and change the IP address to test. The code snippet is as follows:

[java] view plaincopyprint&#63;
   //HTTP上传图片 
RequestParams params = new RequestParams(); 
try { 
  //将压缩后的bitmap保存为图片文件 
  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(); 
  //上传压缩后的文件,大约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(); 
} 
//上传地址 
    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(int status, Header[] arg1, String json) { 
    super.onSuccess(status, arg1, json); 
    LogUtil.e(TAG, "上传图片 json :"+json); 
    RespondBaseEntity entity=GsonUtil.GetFromJson(json, RespondBaseEntity.class); 
    if(entity.isStatus()){ 
      //上传成功,设置图片 
      face.setImageBitmap(bmp); 
      ToastUtils.show(context, "上传成功"); 
    }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); 
  } 
Copy after login

params.put("attach", uploadImg); The attach parameters here correspond one-to-one with the server, don't change them randomly. . . .
String url="http://192.168.0.8/upload.php"; This 192.168.0.8 is the address of my PHP deployment, just change it to your own.


PS: Don’t make the mistake 2, use 127.0.0.1 Think about why you can’t use 127.0.0.1

This is building and running. Found OK. . . . You can upload and find the upload directory in the www directory. There are uploaded images in the upload directory. . . .


7. This is puzzling. . . . I plucked up the courage to find the PHP backend. After a heated discussion with him, I found out that the server was cheating me! 800 yuan for a year's server. . . . . well. . . Say no more. . . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1042684.htmlTechArticleDetailed explanation of the process of uploading images to PHP from android. Today, when uploading avatars, a connection timeout error is always submitted. The error message is as follows: XXXXXXSokcetTimeOutXXXXXXXX Then set it yourself...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Motorola Razr 50s shows itself as possible new budget foldable in early leak Motorola Razr 50s shows itself as possible new budget foldable in early leak Sep 07, 2024 am 09:35 AM

Motorola has released countless devices this year, although only two of them are foldables. For context, while most of the world has received the pair as the Razr 50 and Razr 50 Ultra, Motorola offers them in North America as the Razr 2024 and Razr 2

See all articles