【问题标题】:PlatformException(400, HTTP status code error., null)PlatformException(400, HTTP 状态码错误。, null)
【发布时间】:2021-01-02 19:08:21
【问题描述】:

我正在尝试从 api 下载图像,不幸的是它显示错误。更改网址效果很好。我该如何解决这个问题?有没有其他方法可以下载这种类型的图像?

这是我用于下载图像的代码:

_downloadandSaveImage() async {
      try {
        showdownloading = true;

        // Saved with this method.
        print(widget.imageUrl);
        print(_progress);
        //var imageId = await ImageDownloader.downloadImage(widget.imageUrl);
        var imageId = await ImageDownloader.downloadImage("http://wallpaper.pkappstudio.info/upload/47418_animal2.jpg",
            destination: AndroidDestinationType.directoryDCIM
        );
        Toast.show("Saved!", context, duration:  Toast.LENGTH_SHORT, gravity:  Toast.BOTTOM);
        if (imageId == null) {
          return;
        }

      } on PlatformException catch (error) {
        print(error);
      }

    }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我只需要在我的 androidManifest.xml 文件中将 usesClearTexttraffic 设置为 true。

    【讨论】:

      【解决方案2】:

      错误来自链接本身。这意味着链接无效。它返回状态码 400。这就是 http 状态码 400 的含义:

      超文本传输​​协议 (HTTP) 400 Bad Request 响应状态代码表示服务器不能或不会处理请求,因为某些原因被认为是客户端错误(例如,格式错误的请求语法、无效的请求消息帧、或欺骗性的请求路由)。

      感谢 Mozilla 对状态码 400 的出色解释:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

      检查一下,以便您能够处理将来可能遇到的其他状态代码:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

      【讨论】:

      • 是的,我知道这个错误..但是当我在同一个 url 中使用 Image.network 时,它会显示图像..,所以,如果在查看图像时没有发生错误那么为什么它在下载图像时发生错误
      • 我完全不确定当您收到状态 400 错误时您是如何下载图像的。我认为正在显示图像,因为您的应用尚未热重新加载。尝试在终端中运行 flutter clean 并检查 url 是否有效。
      • 请不要对我的回答投赞成票。我正在努力获得无名英雄徽章。我需要得到 0 个赞成的答案并接受该答案。谢谢。
      • okey..我清理并重新加载它,但网址仍然有效
      • 并请编辑您的编辑,您回答一点。除非赞成票未设置为 0
      猜你喜欢
      • 1970-01-01
      • 2019-09-13
      • 2017-09-07
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2018-08-09
      • 2021-01-01
      • 2012-05-30
      相关资源
      最近更新 更多