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

WBOY
Release: 2016-07-13 09:45:11
Original
751 people have browsed it

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...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!