0
getData() {
return fetch("http://bitcfeedcms.rf.gd/script.php")
  .then(response => {
    console.log(response);
    response.json();
  })
  .then(responseJson => {
    this.setState({ data: responseJson });
  })
  .catch(error => {
    console.error(error);
  });

}

I also tried by putting ?l=1 like " bitcfeedcms.rf.gd/script.php?l=1 ". The main json file is " bitcfeedcms.rf.gd/feed_data.json ". So i tried "http://bitcfeedcms.rf.gd/feed_data.json?l=1" this too but nothing changed

What i have to do now to get the json data and use it in my app...Please help

Debotos Das
  • 519
  • 1
  • 6
  • 17

3 Answers3

4

You are using the arrow function wrong. Instead of this:

fetch("http://bitcfeedcms.rf.gd/script.php")
  .then(response => {
    console.log(response);
    response.json();
  })
  .then(responseJson => {
    this.setState({ data: responseJson });
  })

You should return the response.json()

fetch("http://bitcfeedcms.rf.gd/script.php")
   .then(response => {
     console.log(response);
     return response.json();
   })
   .then(responseJson => {
     this.setState({ data: responseJson });
   })

This way you can reach responseJson in the next then.

Also, if your app complains about fetch() network request failed probably it's about Info.plist or Manifest configuration error. See this topic.

For iOS, you can try the same request with this https dummy json url: https://jsonplaceholder.typicode.com/posts/1

eden
  • 5,876
  • 2
  • 28
  • 43
  • http://bitcfeedcms.rf.gd/1.png here the response data. it's not giving the correct data.... – Debotos Das Aug 15 '17 at 17:11
  • yes i tried with your code then the (screenshot) result appeared. – Debotos Das Aug 16 '17 at 01:21
  • http://bitcfeedcms.rf.gd/feed_data.json?l=1 try with this. Console log both `then` and tell me the outputs. – eden Aug 16 '17 at 08:18
  • http://bitcfeedcms.rf.gd/up1.png http://bitcfeedcms.rf.gd/up2.png As you told...i did and the outcome is in the screenshots – Debotos Das Aug 21 '17 at 18:23
  • check the comment on the question written by @MattyK14 – eden Aug 21 '17 at 21:33
  • I tried via reloading package....nothing happen. And the postman giving me html as response....see here the response of postman http://bitcfeedcms.rf.gd/postman%20error.png – Debotos Das Aug 22 '17 at 16:06
  • @DebotosDas that's the main reason of failure. You should want your api to return JSON only. – eden Aug 22 '17 at 17:04
  • How do i do that ....will you give me a demo or example link. I want to add the form (http://bitcfeedcms.rf.gd) data in the json file and access that json file from my react-native app....that's all – Debotos Das Aug 23 '17 at 16:23
0

http://bitcfeedcms.rf.gd/script.php , this link returns multiple sets of JSON.

([{"FeedTitle":"Debotos","FeedDescription":"First Feed Testing....."},{"FeedTitle":"Akash","FeedDescription":"Creating a clan named \"Khulna Sparkers\""},{"FeedTitle":"Ripon","FeedDescription":"My brother and my one of the closest individual"}])

try this . . .

getData() {
 fetch("http://bitcfeedcms.rf.gd/script.php")
  .then(response => {
    console.log(response);
    response.json();
  })
  .then(responseJson => {
        var length = responseJson.length;
        for (var a = 0; a<length;a++) {
            var FeedTitle = responseJson[a].FeedTitle; //<-variable from your response json
            var FeedDescription = responseJson[a].FeedDescription; //<-from your response json
            console.log(FeedTitle);
            console.log(FeedDescription);
        }
  })
  .catch(error => {
    console.error(error);
  });
}
  • Response log is here http://bitcfeedcms.rf.gd/1.png other url (for script.php) is responding the same thing like that one – Debotos Das Aug 15 '17 at 17:08
  • take a look at this line {"FeedTitle":"Akash","FeedDescription":"Creating a clan named \"Khulna Sparkers\""} i guess you have a wrong format of Json. json data must be like this {"var":"value"} – Josef Guillen Aug 17 '17 at 00:57
  • but at offline(via directory path) when i am trying to load that json file it works perfectly....so the json format may be not wrong, it's an array of objects – Debotos Das Aug 17 '17 at 02:34
0

try axios

npm install --save axios

state = {data:[]};
    componentWillMount() {
                 axios.get('http://bitcfeedcms.rf.gd/script.php')
                 .then(response =>this.setState({data:response.data}));
    }