HTTPUrlConnection 出现 FileNotFoundException 的问题

本文探讨了使用HTTPUrlConnection时遇到FileNotFoundException的原因及解决方案。一种情况是URL含有中文字符,可通过URLEncoder.encode方法解决;另一种是服务器返回400及以上错误码时,应检查getErrorStream而非getInputStream。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.getInputStream()



HTTPUrlConnection 在 getInputStream出现 FileNotFoundException 的问题,可能有两种情况:

1. URL中带有中文可能导致这个问题。具体分析待补充。使用 URLEncoder.encode(..)解决。

参看这里 http://blog.youkuaiyun.com/berber78/article/details/7245872


2. URL确定OK的话,还是出现FileNotFoundException ,可以看看是否是如下情况:

FileNotFoundException also comes when you get an error message from the server and then try to access getInputStream(). If the Response code is a 400 or above, any call to getInputStream() will throw this error. In this case, you should check getErrorStream() instead. See my answer here.


if ( conn.getResponseCode() >= 400) {
    conn.getErrorStream();
} else {
    conn.getInputStream();
}


参看这里:

http://stackoverflow.com/questions/9472425/filenotfoundexception-on-android-version-2-3/18151461#18151461

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zonson9999

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值