Axios get error - React Native

Asked

Viewed 2,042 times

1

I’m taking a React Native course and I’m not able to make an HTTP request using Axios.

This is my code:

import React, { Component } from 'react'
import { View } from 'react-native'

import axios from 'axios'
import Itens from './Itens'

class ListaItens extends Component {
  componentWillMount () {
    axios.get('http://faus.com.br/recursos/c/dmairr/api/itens.html')
      .then(response => { console.log(response) })
      .catch((error) => console.log('Erro ao recuperar os dados ' + error))
  }
  render () {
    return (
      <View>
        <Itens />
        <Itens />
        <Itens />
      </View>
    )
  }
}
export default ListaItens

And the return of the catch error is just Network Error

I saw that some people had the problem solved when the service to be contacted was on localhost

I’m using the Android emulator to do the tests and I don’t know if any configuration has to be done in it for the request to work, but I can use the internet with it normally.

  • 1

    You can try it online at: https://snack.expo.io/@sant0will/sponaneous-nachos. Apparently it’s coming right on the console.log. Maybe this Network error is because it is not connected to the internet.

  • Are you making this request using a wireless connection? Example: On your pc/notebook, you are connected to a Wi-Fi network instead of wiring. Are you really able to use the internet in the emulator (e.g., Google Chrome)? - He’s taking that Udemy course that creates a list of products, right? :)

  • That’s right, @Renan. It’s Udemy’s course. I’m connecting over wifi and being able to use the internet from the emulator. Access any site normally.

  • So it’s different from the problem I had, in my case I couldn’t access any site, zero connection. I decided to create a batch to clean the emulator data and to go up the emulator by setting the dns-server. emulator.exe -avd Nexus_5_API_27 -wipe-data and emulator.exe -avd Nexus_5_API_27 -dns-server 8.8.8.8. You can do a test using this second command and by changing Nexus_5_API_27 by the emulator it has.

  • Are you sure your Endpoint is right? http://faus.com.br/recursos/c/dmairr/api/itens.html Why should he be like this I think http://faus.com.br/recursos/c/dmairr/api/itens

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.