使用本地响应将图像发送到服务器时,网络请求失败

时间:2019-07-19 12:39:16

标签: image react-native post server fetch

我想将图像发送到服务器并以json格式获取结果,但是应用程序返回网络请求失败错误

  

使用genymotion作为模拟器来反应本机0.6

我尝试了RNFetchblob,但结果需要很长时间才能获得响应(5分钟) 我也尝试过axios,但它以200 ok响应空数据

这是导入图像的功能

 OnClick = () => {
    ImagePicker.showImagePicker(options, response => {
      console.log("Response = ", response);

      if (response.didCancel) {
        console.log("User cancelled image picker");
      } else if (response.error) {
        console.log("Image Picker Error: ", response.error);
      } else {
        let source = { uri: response.uri };

        // You can also display the image using data:
        //let source = { uri: 'data:image/jpeg;base64,' + response.data };

        this.setState({
          avatarSource: source,
          data: response.data,
          BtnDisabled: false
        });
        console.log();
      }
    });
  };

以及这种发送图像的方法

 Send = async () => {


     let url = "http://web001.XXX.com:8000/api/prediction/check_prediction/";
     let UplodedFile = new FormData();
     UplodedFile.append('file',{  type:'image/jpeg', uri :  this.state.avatarSource , name:'file.jpeg'});
     fetch(url, {
      method: 'POST',
      body:UplodedFile

     })
     .then(response => response.json())
     .then(response => {
       console.log("success");

        console.log(response);
      })
      .catch(error => {
        console.error(error);
      });

我希望json格式

ScreenShot here

1 个答案:

答案 0 :(得分:0)

您可以这样更改代码吗?

OnClick = () => {
    ImagePicker.showImagePicker(options, response => {
      console.log("Response = ", response);

      if (response.didCancel) {
        console.log("User cancelled image picker");
      } else if (response.error) {
        console.log("Image Picker Error: ", response.error);
      } else {
        let source = { uri: response.uri };

        // You can also display the image using data:
        //let source = { uri: 'data:image/jpeg;base64,' + response.data };

        this.setState({
          pickerResponse: response,
          data: response.data,
          BtnDisabled: false
        });
        console.log();
      }
    });
  };
Send = async () => {
     let url = "http://web001.XXX.com:8000/api/prediction/check_prediction/";
     let UplodedFile = new FormData();
     UplodedFile.append('file',{  type:'image/jpeg', uri : this.state.pickerResponse.path , name:'file.jpeg'});
     axios({
        method: "post",
        url: url,
        data: UplodedFile
      })
     .then(response => {
       console.log("success");

        console.log(response);
      })
      .catch(error => {
        console.error(error);
      });