java.io.FileNotFoundException: http://192.168.43.127:9090/api_request/query_weather?apiKey=06f509e58

问题

笔者进行Android 项目开发 ,控制台报错

java.io.FileNotFoundException: http://192.168.43.127:9090/api_request/query_weather?apiKey=06f509e58

详细问题

客户端(Android)请求Web端代码

// 查询天气方法
    public static String queryWeather(String apiKey, String cityCode) {
        try {
            // 构建请求的URL
            URL url = new URL("http://192.168.43.127:9090/api_request/query_weather?apiKey=" + apiKey + "&cityCode=" + cityCode);

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求类型为GET
            connection.setRequestMethod("GET");

            // 获取响应
            InputStream inputStream = connection.getInputStream();
            String response = convertStreamToString(inputStream);
            // 处理响应,这里假设响应是JSON格式的数据
            System.out.println(response);
            return response;

        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常
            return null;
        }
    }

Web端请求API服务端代码

@GetMapping("/query_weather")
    public static String queryWeather(String apiKey, String cityCode) {
        StringBuilder response = new StringBuilder();
        try {
            // 构造请求 URL
            String urlString = "https://restapi.amap.com/v3/weather/weatherInfo?key=" +
                    URLEncoder.encode(apiKey, "UTF-8") +
                    "&city=" +
                    URLEncoder.encode(cityCode, "UTF-8") +
                    "&extensions=all&output=JSON";

            // 发送 GET 请求
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 读取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.toString();
    }

控制台详细报错

2024-02-10 21:33:29.005 6792-6820/com.example.assistingagriculture W/System.err: java.io.FileNotFoundException: http://192.168.43.127:9090/api_request/query_weather?apiKey=06f509e58a.................&cityCode=110108
2024-02-10 21:33:29.005 6792-6820/com.example.assistingagriculture W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255)
2024-02-10 21:33:29.005 6792-6820/com.example.assistingagriculture W/System.err:     at com.example.assistingagriculture.Utils.DB.queryWeather(DB.java:360)
2024-02-10 21:33:29.005 6792-6820/com.example.assistingagriculture W/System.err:     at com.example.assistingagriculture.activity.care_model.SelectingCropsActivity1$GetCropBinding.doInBackground(SelectingCropsActivity1.java:54)
2024-02-10 21:33:29.005 6792-6820/com.example.assistingagriculture W/System.err:     at com.example.assistingagriculture.activity.care_model.SelectingCropsActivity1$GetCropBinding.doInBackground(SelectingCropsActivity1.java:45)
2024-02-10 21:33:29.006 6792-6820/com.example.assistingagriculture W/System.err:     at android.os.AsyncTask$3.call(AsyncTask.java:394)
2024-02-10 21:33:29.006 6792-6820/com.example.assistingagriculture W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2024-02-10 21:33:29.006 6792-6820/com.example.assistingagriculture W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
2024-02-10 21:33:29.006 6792-6820/com.example.assistingagriculture W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2024-02-10 21:33:29.006 6792-6820/com.example.assistingagriculture W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2024-02-10 21:33:29.006 6792-6820/com.example.assistingagriculture W/System.err:     at java.lang.Thread.run(Thread.java:923)

解决方案

步骤一、检查服务器IP地址是否正确
步骤二、检查请求路径是否正确
步骤二、检查请求参数是否被正确发送,接收
对于笔者而言,由于web端没有正确接收Android端发送的请求参数报错

web端方法定义由

public static String queryWeather(String apiKey, String cityCode)

修改为

public static String queryWeather(@RequestParam String apiKey, @RequestParam String cityCode)

增加 @RequestParam注解,保证参数被正确接收
在这里插入图片描述

产生原因

问题产生的原因可以分为两个方面:
服务器地址错误或不可访问: 控制台报错中显示了 java.io.FileNotFoundException,这表示在请求指定的 URL 时未找到对应的资源。这可能是由于服务器地址错误、服务器未启动或者服务器端口被防火墙阻挡等原因导致的。因此,首先应该检查服务器地址是否正确,并且确保服务器处于运行状态。
请求路径或参数错误: 如果服务器地址正确,但仍然收到 FileNotFoundException 错误,那么可能是由于请求路径或参数不正确导致的。在你的解决方案中,通过添加 @RequestParam 注解来确保在 Web 端正确接收 Android 端发送的请求参数是一个有效的方法。这确保了服务器能够正确解析并处理客户端发送的请求,从而避免了文件未找到的错误。

解决原因

通过在服务器端的方法参数中添加 @RequestParam 注解,确保了服务器能够正确解析并处理客户端发送的请求参数,从而解决了文件未找到的错误。同时,还需要确保客户端发送的请求中 URL 地址正确,服务器能够正确识别请求的路径。

参考文献

产生原因以及解决原因部分内容 部分参考chatgpt

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
请添加图片描述

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

飞滕人生TYF

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

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

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

打赏作者

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

抵扣说明:

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

余额充值