简体   繁体   English

AxiosError:Android React Native Expo 上的网络错误

[英]AxiosError: Network Error on Android React Native Expo

I keep receiving the Axios Network Error while trying to do get request.我在尝试获取请求时不断收到 Axios 网络错误。 Everything is working perfectly fine on iOS.在 iOS 上一切都运行良好。 Its only on android where this occurs.它仅在发生这种情况的android上。 On the emulator I noticed when I can change the base url to include 10.0.2.2, it works, but not on the acutal device.在模拟器上,我注意到当我可以将基本 url 更改为包含 10.0.2.2 时,它可以工作,但在实际设备上却不行。 Here is my current setup这是我目前的设置

base url is http://localhost:8888基本网址是 http://localhost:8888

 const fetchComponent = (props) => {
  const [data, setData] = useState([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetchData();
  }, []);

  const fetchData = async () => {
    try {
      console.log("props received:", props.link);
      const configurationObject = {
        method: "get",
        url: props.link,
      };
      const response = await axios(configurationObject);

      console.log("response:", response);
      if (response.status === 200) {
        setData(response.data);
        console.log(response.data);
      }
      setLoading(false);
    } catch (error) {
      console.log(error.response);
      setLoading(false);
    }
  };

  return { data, loading };
};
const fetchComponent = (props) => {
  const [data, setData] = useState([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetchData();
  }, []);

  const fetchData = async () => {

      console.log("props received:", props.link);
axios.get(`${props.link}`)
.then(data=>setData(data.data)
.catch(err=>setLoading(false))
  };

  return { data, loading };
};

try like this试试这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM