【问题标题】:nativescript-background-http get response bodynativescript-background-http 获取响应正文
【发布时间】:2016-07-20 14:24:43
【问题描述】:

我目前正在使用 nativescript-background-http 上传图片,我只是想知道有没有办法在发送图片后从服务器获取响应正文或响应标头?

【问题讨论】:

  • 嘿,您找到获取响应正文的方法了吗?

标签: nativescript


【解决方案1】:

以防一年后有人遇到同样的问题:

task.on("responded", (e) => {
   JSON.parse(e.data)
}

【讨论】:

    【解决方案2】:

    我能够从服务器获取响应的方法是在完整方法中使用 getBodyAsString() 方法并对其进行解析。

     task.on("complete", (ev: any) =>  {
                    console.log("Upload complete");
                    let sr = JSON.parse(ev.response.getBodyAsString());                  
             });
    

    【讨论】:

      【解决方案3】:
      function sendImages(uri, fileUri) {
      
          imageName = extractImageName(fileUri);
      
          var request = {
              url: "http://httpbin.org/post",
              method: "POST",
              headers: {
                  "Content-Type": "application/octet-stream",
                  "File-Name": imageName
              },
              description: "{ 'uploading': " + imageName + " }"
          };
      
          var task = session.uploadFile(fileUri, request);
      
          task.on("progress", logEvent);
          task.on("error", logEvent);
          task.on("complete", logEvent);
      
          function logEvent(e) {      
              console.log("----------------");
              console.log('Status: ' + e.eventName);
              // console.log(e.object);
              if (e.totalBytes !== undefined) {
                  console.log('current bytes transfered: ' + e.currentBytes);
                  console.log('Total bytes to transfer: ' + e.totalBytes);
              }  
          }
      
          return task;
      }
      

      基于this demo

      【讨论】:

      • 但是来自服务器的响应头呢?
      猜你喜欢
      • 2018-03-27
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-19
      相关资源
      最近更新 更多