Android to React Native:TypeError:网络请求失败

时间:2017-01-31 18:46:40

标签: javascript java android https react-native

我正在尝试将Android应用转换为React Native,我在Android应用上遇到了以下API请求之一:

public Result checkModule(Bundle params) {
  String strURL = 

  HttpURLConnection conn = null;

  try {
    URL url = new URL(strURL);
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod(HttpVerb.GET.name());
    setBasicAuth(conn);
    conn.connect();
    Log.d("Req", "[" + conn.getRequestMethod() + "] to " + url.toString());

    String body = readStream(conn.getInputStream());
    return new Result(conn.getResponseCode(), body);
  }
}

我尝试将其转换为React Native:

export const practiceFetch = () => {
  var requestHeader = {
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded'
  }

  var request = {
    method: 'GET',
    headers: requestHeader,
  }

  return dispatch => {
    fetch('https://apipractice.io:3000/monitor/module', request)
    .then(res => res.json())
    .then(payload => {
      console.log(‘Return success’)
      console.log(payload)

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

但我收到错误TypeError: Network request failed。我尝试添加<key>NSAllowsArbitraryLoads</key><true/>,但收到同样的错误。

错误可能是什么问题?我怎样才能正确地从Android转换为React Native?

0 个答案:

没有答案