2016-07-30 36 views
6

Zajmuję się tworzeniem aplikacji na Androida za pomocą React Native. Testuję swoją aplikację na interfejsie API Galaxy Nexus 23, który emuluje się w Android Studio, działającym na komputerze Mac.React Native na Androida, wywołanie ajax z pobierania nie powiedzie się

Chciałbym umożliwić użytkownikom dzielenie się informacjami. Używam serwera WWW z php i mysql i chcę, aby aplikacja wysyłała do serwera żądania udostępniania danych między użytkownikami.

Próbuję wysłać żądanie POST do mojego serwera programowania przy użyciu interfejsu API pobierania. Mój kod js jest poniżej:

var AjaxEngine = { 
    test: function() { 
      return fetch('http://10.0.2.2/test.json') 
       .then((response) => response.json()) 
       .then((responseJson) => { 
        console.log(responseJson); 
        return responseJson.movies; 
       }) 
       .catch((error) => { 
        console.error(error); 
       }); 
     } 
}; 

nazywany przez AjaxEngine.test();

W moim emulatora otrzymuję następujący błąd:

ExceptionsManager.js:70 TypeError: Network request failed at XMLHttpRequest.xhr.onerror (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:23711:8) at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:9740:15) at XMLHttpRequest.setReadyState (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:25157:6) at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:25015:6) at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:25090:52 at RCTDeviceEventEmitter.emit (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:8977:23) at MessageQueue.__callFunction (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:7065:23) at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:6969:8 at guard (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:6917:1) at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:6968:1)

test.json składa się z:

{ 
    "message":"hello world" 
} 

Żądanie powiodło się po zastąpieniu 'http://10.0.2.2/test.json' przez 'http://facebook.github.io/react-native/movies.json', więc sama funkcja jest w porządku.

'http://localhost/test.json' i 'http://127.0.0.1/test.json' zarówno obciążenie w przeglądarce internetowej, curl i wget. Próbowałem również zastąpienie 10.0.2.2 z 127.0.0.1. Próbowałem również użyć mojego lokalnego adresu IP (129.xxx.x.x)

Co muszę zrobić, aby te żądania zadziałały? Jak uzyskać dostęp do mojego serwera programistycznego działającego na localhost z emulowanej aplikacji dla systemu Android?

Odpowiedz

0

Jeśli sprawisz, że curl zadziała, wypróbuj opcję -D, która zapisuje nagłówki do pliku. W ten sposób możesz sprawdzić, czy oba żądania mają f.ex. ten sam typ mime.

-D, --dump-header <file> 
      Write the protocol headers to the specified file. 

      This option is handy to use when you want to store the headers 
      that an HTTP site sends to you. Cookies from the headers could 
      then be read in a second curl invocation by using the -b, 
      --cookie option! The -c, --cookie-jar option is a better way to 
      store cookies.