Client/ReactJS & React-Native

adb) 안드로이드 에뮬레이터 [TypeError: Network request failed].

Juzdalua 2022. 2. 9. 11:32

RN에서 백엔드로 fetch 또는 axios로 데이터를 요청할 때 에러가 발생한다.

 

1. fetch의 URL을 localhost가 아닌 ip로 입력해준다.

fetch('http://127.0.0.1:4000', {
      method: 'POST',      
      body: JSON.stringify(data),
      headers: {
        'Content-Type': 'application/json',
      },
      redirect: 'follow',
    })

 

2. android/app/src/main/AndroidManifest.xml 에 코드를 추가한다

android:usesCleartextTraffic="true"

 

3. adb 안드로이드 에뮬레이터에 백엔드 서버포트를 매핑해준다.

에뮬레이터가 실행된 상태에서 터미널에 아래와 같이 입력한다.

 

먼저 에뮬레이터 이름을 알아보자.

adb devices

 

그리고 서버 포트를 매핑해보자.

adb -s 에뮬레이터이름 reverse tcp:포트번호 tcp:포트번호

ex)adb -s emulator-5554 reverse tcp:4000 tcp:4000

된다...