gpt4 book ai didi

java.io.FileNotFoundException : When using real server 错误

转载 作者:行者123 更新时间:2023-11-30 00:55:06 24 4
gpt4 key购买 nike

我是 Java 和 Android Studio 的初学者。我用 Android Studio 和 Wamp 作为服务器编写了代码,Genymotion 作为模拟器编写了代码。所有代码都工作正常,我可以使用我的 .php 文件与 mysql 交互

然后我决定将代码传输到真实服务器。

但是我得到这个错误:java.io.FileNotFoundException:http://burj.1shahrvand.com/Burj/BikerLogin.php

文件可用check it here但是我得到了找不到文件的异常

代码是这样的:

String uri = rp.getUri();
if(rp.getMethod().equals("GET")){
uri += "?" + rp.getEncodedParams();
}


HttpURLConnection connection;
try {
URL url = new URL(uri);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(rp.getMethod());
if (rp.getMethod().equals("POST")){
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(rp.getEncodedParams());
writer.flush();


}
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();

while ((line = reader.readLine()) != null) {
sb.append(line);

}
return sb.toString();


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
Log.i("HESAM Original", e.getMessage());
e.printStackTrace();
}


return null;

感谢您的帮助!

最佳答案

如果您在服务器响应 404 或 410 状态代码后调用 getInputStream,您将得到一个 FileNotFoundException

如果要避免异常,请检查响应状态代码是否为 2xx 代码。如果不是,则使用 getErrorStream 而不是 getInputStream

关于java.io.FileNotFoundException : When using real server 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40321227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com