今天在做上传头像的时候,总是提交连接超时错误,报错信息如下:XXXXXXSokcetTimeOutXXXXXXXX
然后自己设置HTTP的超时时间:
代码如下:
[java] view plaincopyprint?
//设置超时时间
httpclient.setTimeout(20000);
再building,runing,还是不行。。。。这就怪了,明明好好的,怎么会突然就变成连接超时了呢!又折腾了一阵子后,也跟后台那边的朋友沟通过,他也测试了上传接口,发现没什么问题,就让我自己去折腾去了。。。。
我就郁闷了,看不出原先的代码有什么错误,也没什么法子了,就出最下下策吧,自己搭一个PHP上传图片接口,亲自测试下到底是怎么回事。。。。
1.首先,你得下一个方便快捷的PHP服务器,我这里用了WampServer,百度----下载-----安装-----启动,浏览器输入:http://127.0.0.1 有页面显示,OK了。就这么简单!
2.浏览器输入 : http://本机IP地址 回车, 发现报错,类似“You don't have permission to access / on this server” 说明你的WM还没设置,需要进行如下设置:
造成这个问题的原因是Apache 的http.conf内的默认配置是
代码如下:
# onlineoffline tag - don't remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
只允许127.0.0.1访问,点击wampserver图标让后点击Putonline,http.conf内的以上默认配置自动修改为
代码如下:
# onlineoffline tag - don't remove
Order Allow,Deny
Allow from all
现在localhost可以访问了。
同样phpMyadmin在localhost下不能正常访问在127.0.0.1能正常访问,解决方法:
点击根目录下的alias目录,打开phpmyadmin.conf配置文件,和上面修改http.conf一样把
代码如下:
Deny from all
Allow from 127.0.0.1
修改为
Allow from all
3. 再此输入 : http://本机IP地址 回车 显示页面 OK! 至于为什么要第二步、第三步呢,我就不说了。。。留给新人去想想吧! 大神直接无视。。。。。
4.写一个上传图片的PHP文件,当然我一个敲java的孩子一下子怎么可能憋的出来,那怎么办,当然是百度参考别人的了,下面的PHP代码源自网络,亲测没有错误:
[php] view plaincopyprint? <?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 ); } ?>
5.将上面的php文件放在WM安装目录下的www目录下,我的如下图所示,仅供参考:
6.经过上面几个步骤,PHP端已经搭建好了,现在就是回到android端改改IP地址测试下就oK了,代码段如下:
[java] view plaincopyprint? //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); 这里的attach参数是和服务端一一对应的,别乱改。。。。
String url="http://192.168.0.8/upload.php"; 这个192.168.0.8是我的PHP部署的地址,改成你自己的就行了。
PS:别犯2,用了127.0.0.1 想想为啥不能用127.0.0.1
到此就是building,runing了。 发现OK。。。。 可以上传,并在www目录下找到upload目录,upload目录下有上传的图片。。。。
7.这就纳闷了。。。。 我又鼓起勇气找了PHP后端,跟他激烈的讨论一番后,发现是服务器坑了爹啊! 800块一年的服务器。。。。。唉。。。不说了。。。。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

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.

Dreamweaver CS6
Visual web development tools

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),

Zend Studio 13.0.1
Powerful PHP integrated development environment