0

I'm having a problem when I receive my JSON in a React Native app.

I'm doing a simple fetch request to get it

getApplicationList()
{
    return fetch('http://192.168.X.X/index.php')
        .then((response) => response.json())
        .then((responseJson) => {
            return responseJson;
        })
        .catch((error) => {
            return 'There has been a problem with your fetch operation: ' + error.message;
        });
}

but when I make a console.error(getApplicationsList()); in the render function, I'm getting a very bizarre log on the android emulator:

console.error: {"_40":"0","_65":"0","_55":"null","_72":"null"}

In my PHP file, I'm returning a JSON encoded response, and when I type my URL (with 'localhost') in the navigator I can get it.

When I launch the app from the emulator, I change the localhost adress with my computer IP, and when I type this URL in the browser, I'm getting a Cannot GET /index.php response. I don't know if it's alright, and I'm feeling a little lost between all of this.

Many thanks for your help

Koko
  • 89
  • 9
  • it's because `fetch` returns a promise. put the console.error inside the second .then() function and call the method inside render `this.getApplicationList()` – Ray Aug 15 '17 at 16:36
  • I put a console.error in the second .then() but nothing's displayed on the emulator. It seems that it's not going thru... – Koko Aug 15 '17 at 16:47
  • try console logging your response and/or `response.json()` – MattyK14 Aug 15 '17 at 17:09
  • When I console.error in the .then() and .error(), the error window of the emulator doesn't appear. – Koko Aug 15 '17 at 17:16

1 Answers1

0

I found a solution using a real android device:

I followed this to make a new port for my device: Connect an Android Device To a Web Service on Local Host (the answer using the Chrome navigator settings)

and then I made my HTTP request in the app using the IP address of my computer with the port I typed in the previous link. And it worked.

Koko
  • 89
  • 9