网络错误React Native使用Expo

时间:2018-03-22 17:21:46

标签: ios react-native expo

我使用Expo来运行我自己的本机应用程序,但现在我在尝试使用axios时遇到网络错误(我已尝试使用fetch并获得了相同的)连接Indeed URL API。我已经读过iPhone会阻止任何不安全的HTTP请求,除非您在info.plist文件中允许这样做,而该文件可以通过app.json写入。

编辑2: network error from expo,这是我在世博会上获得的,同样在设备上。

这是使用axios的操作。

import axios from 'axios';
//import reverseGeocode from 'latlng-to-zip';
import qs from 'qs';

import {
    FETCH_JOBS
} from './types';

const GOOGLE_API_KEY='HIDDEN API KEY';
const GEOCODE_ROOT_URL = `https://maps.googleapis.com/maps/api/geocode/json?`;

const buildGeocodeUrl = ({ longitude, latitude }) => {
 const query = qs.stringify({ key: GOOGLE_API_KEY, latlng: `${latitude},${longitude}` });
 return `${GEOCODE_ROOT_URL}${query}`;
};

const reverseGeocode = async (region) => {
 const url = buildGeocodeUrl(region);
 let { data } = await axios.get(url);

 if (data.error_message) {
     throw new Error(data.error_message);
 }

 const { results } = data;

 if (results.length === 0) {
     throw new Error('No Results');
 }

 const postCode = results[0].address_components.find(
     component => component.types[0] === 'postal_code'
 );

 if (!postCode) {
     throw new Error('No Postcode');
 }

 return postCode.long_name || postCode.short_name;
};

const JOB_ROOT_URL = 'http://api.indeed.com/ads/apisearch?';
const JOB_QUERY_PARAMS = {
  publisher: 'HIDDEN PUBLISHER',
  format: 'json',
  v: '2',
  latlong: 1,
  radius: 5,
  q: 'javascript'
};

const buildJobsUrl = (zip) => {
  const query = qs.stringify({ ...JOB_QUERY_PARAMS, l:zip });
  return '${JOB_ROOT_URL}${query}';
};

export const fetchJobs = (region) =>  async (dispatch) => {
  try {
    let zip = await reverseGeocode(region);
    const url = buildJobsUrl(zip);
    let { data } = await axios.get(url);
    dispatch({ type: FETCH_JOBS, payload: data });
    console.log(data);
  } catch(error) {
    console.log('soy el error');
    console.error(error);
  }
};

这是我尝试使用我的app.json文件

{
  "expo": {
    "name": "jobs",
    "description": "This project is really great.",
    "slug": "jobs",
    "privacy": "public",
    "sdkVersion": "25.0.0",
    "platforms": ["ios", "android"],
    "version": "1.0.0",
    "orientation": "portrait",
    "icon": "./assets/icon.png",
    "splash": {
      "backgroundColor": "#ffffff"
    },
    "ios": {
      "supportsTablet": true,
      "infoPlist": {
        "NSAppTransportSecurity" : {
          "NSAllowsArbitraryLoads" : ""
        }
      }
    }
  }
}

编辑: 这是我的package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "private": true,
  "dependencies": {
    "axios": "^0.18.0",
    "expo": "^25.0.0",
    "latlng-to-zip": "0.0.1",
    "lodash": "^4.17.5",
    "npm": "^4.6.1",
    "qs": "^6.5.1",
    "react": "16.2.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-25.0.0.tar.gz",
    "react-native-elements": "^0.19.0",
    "react-native-vector-icons": "^4.5.0",
    "react-navigation": "^1.5.8",
    "react-redux": "^5.0.7",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  }
}

感谢任何帮助。

0 个答案:

没有答案