响应时间长时,React本机fetch()在Android上引发“网络请求失败”

时间:2018-07-26 22:14:40

标签: android react-native fetch

fetch()在iOS和Android上都能正常运行,直到一个月前。我发现,如果响应时间较长,则会引发Network request failed错误:

Network request failed
onerror
    whatwg-fetch.js:504:29
dispatchEvent
    event-target.js:172:43
setReadyState
    XMLHttpRequest.js:576:29
__didCompleteResponse
    XMLHttpRequest.js:392:25
<unknown>
    XMLHttpRequest.js:505:8
emit
    EventEmitter.js:191:12
__callFunction
    MessageQueue.js:344:47
<unknown>
    MessageQueue.js:107:26
__guard
    MessageQueue.js:291:8
callFunctionReturnFlushedQueue
    MessageQueue.js:106:17

这是我设置提取请求的方式:

const URLENCODED_HEADER = {
  'Accept': 'application/json',
  'Content-Type': 'application/x-www-form-urlencoded',
}

async function getToken() {
  try {
    let response = await fetch(SERVER_URL+'/secure/token', {
      credentials: 'include'
    });
    let responseJson = await response.json();
    return responseJson.result
  } catch(error) {
    throw error;
  }
}

async function doLogin(secureInfo) {
  formBody = encodeParameters(secureInfo)
  try {
    let response = await fetch(SERVER_URL+'/user/login', {
      method: "POST",
      headers: URLENCODED_HEADER,
      body: formBody,
      credentials: 'include'
    });
    let responseJson = await response.json();
    return responseJson;
  } catch(error) {
    console.error(error);
    throw error;
  }
}

以下是一些我可以成功回复的回复:

1。

err_no:0 结果:BVV7IZVLOYgROXXc

2。

err_no:0
result:{
    login_id:xxxx@gmail.com
    user_name:""
    email: xxx@gmail.com
    user_id:11
    login_type:1
}

但是,其他较长的响应(例如以下响应)导致“网络请求失败”:

{  
   "err_no":0,
   "result":[  
      {  
         "article_id":299,
         "user_id":11,
         "title":"society",
         "content":"",
         "text_content":"\n",
         "tags":"[\"Society\"]",
         "used_articles":"{}",
         "citation_text":"",
         "prompt":"",
         "alternative_title":"",
         "alternative_prompt":"",
         "alternative_cnt":0,
         "image_url":"",
         "created":"2018-07-26 18:28:17",
         "update_time":"2018-07-26 18:28:27"
      }
   ]
}

(从网络平台记录)

是什么原因导致的,我该如何解决?

0 个答案:

没有答案