search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the process of uploading images to PHP from android, _PHP tutorial

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;> 

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); 
  } 

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
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
如何在 iPhone 和 Android 上关闭蓝色警报如何在 iPhone 和 Android 上关闭蓝色警报Feb 29, 2024 pm 10:10 PM

根据美国司法部的解释,蓝色警报旨在提供关于可能对执法人员构成直接和紧急威胁的个人的重要信息。这种警报的目的是及时通知公众,并让他们了解与这些罪犯相关的潜在危险。通过这种主动的方式,蓝色警报有助于增强社区的安全意识,促使人们采取必要的预防措施以保护自己和周围的人。这种警报系统的建立旨在提高对潜在威胁的警觉性,并加强执法机构与公众之间的沟通,以共尽管这些紧急通知对我们社会至关重要,但有时可能会对日常生活造成干扰,尤其是在午夜或重要活动时收到通知时。为了确保安全,我们建议您保持这些通知功能开启,但如果

在Android中实现轮询的方法是什么?在Android中实现轮询的方法是什么?Sep 21, 2023 pm 08:33 PM

Android中的轮询是一项关键技术,它允许应用程序定期从服务器或数据源检索和更新信息。通过实施轮询,开发人员可以确保实时数据同步并向用户提供最新的内容。它涉及定期向服务器或数据源发送请求并获取最新信息。Android提供了定时器、线程、后台服务等多种机制来高效地完成轮询。这使开发人员能够设计与远程数据源保持同步的响应式动态应用程序。本文探讨了如何在Android中实现轮询。它涵盖了实现此功能所涉及的关键注意事项和步骤。轮询定期检查更新并从服务器或源检索数据的过程在Android中称为轮询。通过

如何在Android中实现按下返回键再次退出的功能?如何在Android中实现按下返回键再次退出的功能?Aug 30, 2023 am 08:05 AM

为了提升用户体验并防止数据或进度丢失,Android应用程序开发者必须避免意外退出。他们可以通过加入“再次按返回退出”功能来实现这一点,该功能要求用户在特定时间内连续按两次返回按钮才能退出应用程序。这种实现显著提升了用户参与度和满意度,确保他们不会意外丢失任何重要信息Thisguideexaminesthepracticalstepstoadd"PressBackAgaintoExit"capabilityinAndroid.Itpresentsasystematicguid

Android逆向中smali复杂类实例分析Android逆向中smali复杂类实例分析May 12, 2023 pm 04:22 PM

1.java复杂类如果有什么地方不懂,请看:JAVA总纲或者构造方法这里贴代码,很简单没有难度。2.smali代码我们要把java代码转为smali代码,可以参考java转smali我们还是分模块来看。2.1第一个模块——信息模块这个模块就是基本信息,说明了类名等,知道就好对分析帮助不大。2.2第二个模块——构造方法我们来一句一句解析,如果有之前解析重复的地方就不再重复了。但是会提供链接。.methodpublicconstructor(Ljava/lang/String;I)V这一句话分为.m

如何在2023年将 WhatsApp 从安卓迁移到 iPhone 15?如何在2023年将 WhatsApp 从安卓迁移到 iPhone 15?Sep 22, 2023 pm 02:37 PM

如何将WhatsApp聊天从Android转移到iPhone?你已经拿到了新的iPhone15,并且你正在从Android跳跃?如果是这种情况,您可能还对将WhatsApp从Android转移到iPhone感到好奇。但是,老实说,这有点棘手,因为Android和iPhone的操作系统不兼容。但不要失去希望。这不是什么不可能完成的任务。让我们在本文中讨论几种将WhatsApp从Android转移到iPhone15的方法。因此,坚持到最后以彻底学习解决方案。如何在不删除数据的情况下将WhatsApp

同样基于linux为什么安卓效率低同样基于linux为什么安卓效率低Mar 15, 2023 pm 07:16 PM

原因:1、安卓系统上设置了一个JAVA虚拟机来支持Java应用程序的运行,而这种虚拟机对硬件的消耗是非常大的;2、手机生产厂商对安卓系统的定制与开发,增加了安卓系统的负担,拖慢其运行速度影响其流畅性;3、应用软件太臃肿,同质化严重,在一定程度上拖慢安卓手机的运行速度。

Android中动态导出dex文件的方法是什么Android中动态导出dex文件的方法是什么May 30, 2023 pm 04:52 PM

1.启动ida端口监听1.1启动Android_server服务1.2端口转发1.3软件进入调试模式2.ida下断2.1attach附加进程2.2断三项2.3选择进程2.4打开Modules搜索artPS:小知识Android4.4版本之前系统函数在libdvm.soAndroid5.0之后系统函数在libart.so2.5打开Openmemory()函数在libart.so中搜索Openmemory函数并且跟进去。PS:小知识一般来说,系统dex都会在这个函数中进行加载,但是会出现一个问题,后

Android APP测试流程和常见问题是什么Android APP测试流程和常见问题是什么May 13, 2023 pm 09:58 PM

1.自动化测试自动化测试主要包括几个部分,UI功能的自动化测试、接口的自动化测试、其他专项的自动化测试。1.1UI功能自动化测试UI功能的自动化测试,也就是大家常说的自动化测试,主要是基于UI界面进行的自动化测试,通过脚本实现UI功能的点击,替代人工进行自动化测试。这个测试的优势在于对高度重复的界面特性功能测试的测试人力进行有效的释放,利用脚本的执行,实现功能的快速高效回归。但这种测试的不足之处也是显而易见的,主要包括维护成本高,易发生误判,兼容性不足等。因为是基于界面操作,界面的稳定程度便成了

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),