简体   繁体   English

React-Native-Uploader Android错误

[英]React-native-uploader Android Errors

I'm currently trying to debug a react-native package ( react-native-uploader ) I'm using to try and upload a bundle of files (photos). 我目前正在尝试调试一个react-native包( react-native-uploader ),该包用于尝试上传一捆文件(照片)。 Despite working on ios, the current implementation is returning the following error for android: 尽管在ios上工作,但当前的实现针对android返回以下错误:

Response{protocol=http/1.1, code=405, message=Method Not Allowed, url=${config.apiBase}/load/${this.props.id}/uploadconfirmation}

The error is originating from this line in the package: 错误是由软件包中的这一行引起的:

Response response = client.newCall(request).execute();

Where the client is: 客户在哪里:

private final OkHttpClient client = new OkHttpClient()

Where request is: 请求在哪里:

Request{method=POST, url=${config.apiBase}/load/${this.props.id}/uploadconfirmation, tag=null}

I've successfully made posts to the endpoint using formdata: 我已经使用formdata成功地发布了到端点:

    let tData = new FormData();
    const that = this;

    tData.append("confirmation_doc", {
      uri: files[0].filepath,
      type: "image/jpeg",
      name: "confirmation_doc.jpg",
    });    

    axios.post(
      `${config.apiBase}/load/${this.props.id}/uploadconfirmation`,
           tData
    )
    .then(response => {
        Alert.alert(
          "Success",
          "Uploaded Successfully!",
          [{ text: "OK", onPress: () => that.props.close() }],
          { cancelable: false }
        );
    });

I've tried looking through the source code to determine where things are falling apart and it seems like everything is posting as it should (headers look good, method looks good, endpoint looks good). 我尝试查看源代码,以确定什么地方崩溃了,似乎一切都按预期发布(标题看起来不错,方法看起来不错,端点看起来不错)。 I'm not all too familiar with Java so any input would be appreciated. 我对Java不太熟悉,因此不胜感激。

HTTP 405 Method Not Allowed ... is a client-side error. HTTP 405 Method Not Allowed ...是客户端错误。

The method received in the request-line is known by the origin server but not supported by the target resource. 在请求行中接收到的方法是源服务器已知的,但目标资源不支持。

if the JavaScript works, but the Java won't... you might be looking for the MultipartBuilder 如果JavaScript可以运行,但是Java无法运行...您可能正在寻找MultipartBuilder

... in combination with MediaType.FORM . ...与MediaType.FORM结合使用。

In order to solve this issue, I had to abandon the react-native-uploader package I had been using. 为了解决此问题,我不得不放弃了我一直在使用的react-native-uploader软件包。 Below is how I managed to resolve the issue: 以下是我如何解决此问题的方法:

let tData = new FormData();

this.state.selectedImages.forEach((item, i) => {
  tData.append("doc[]", {
    uri: item.uri,
    type: "image/jpeg",
    name: item.filename || `filename${i}.jpg`,
  });
});

fetch(`${config.apiBase}/load/${this.props.id}/uploadconfirmation`, {
  method: "post",
  headers: {
    Accept: "application/x-www-form-urlencoded",
    Authorization: `Token ${this.props.token}`,
  },
  body: tData,
})
  .then(res => res.json())
  .then(res => {
    Alert.alert(
      "Success",
      "Uploaded Successfully!",
      [{ text: "OK", onPress: () => that.props.close() }],
      { cancelable: false }
    );
  })
  .catch(err => {
    console.error("error uploading images: ", err);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM