簡體   English   中英

React-Native-Uploader Android錯誤

[英]React-native-uploader Android Errors

我目前正在嘗試調試一個react-native包( react-native-uploader ),該包用於嘗試上傳一捆文件(照片)。 盡管在ios上工作,但當前的實現針對android返回以下錯誤:

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

錯誤是由軟件包中的這一行引起的:

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

客戶在哪里:

private final OkHttpClient client = new OkHttpClient()

請求在哪里:

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

我已經使用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 }
        );
    });

我嘗試查看源代碼,以確定什么地方崩潰了,似乎一切都按預期發布(標題看起來不錯,方法看起來不錯,端點看起來不錯)。 我對Java不太熟悉,因此不勝感激。

HTTP 405 Method Not Allowed ...是客戶端錯誤。

在請求行中接收到的方法是源服務器已知的,但目標資源不支持。

如果JavaScript可以運行,但是Java無法運行...您可能正在尋找MultipartBuilder

...與MediaType.FORM結合使用。

為了解決此問題,我不得不放棄了我一直在使用的react-native-uploader軟件包。 以下是我如何解決此問題的方法:

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