反应本机提取请求失败

时间:2018-10-15 13:11:52

标签: android react-native request fetch

我不知道自己做了什么更改,但是提取请求不再起作用(仅使用Android)。它曾经工作过,曾试图将其改回,但无法使其正常工作。

我尝试了此问题中的链接:React native network request failed while fetching data

我使用正确的IP地址。 (像往常一样,将Ipconfig IPv4地址作为URL)。设备的android调试服务器主机和端口上的Dev设置为空,设备通过USB电缆连接。当我在浏览器中输入IP地址时,它会显示正确的JSON数据。

我尝试使用Remote JS Debugging对其进行调试,但是没有找到失败原因的信息。 必须在'http://'->'http://'之间添加空格,因为我不允许提供大量链接错误消息说:

  

可能的未处理的承诺拒绝(id:0):   TypeError:网络请求失败   TypeError:网络请求失败       在XMLHttpRequest.xhr.onerror(blob:http:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:15827:18)       在XMLHttpRequest.dispatchEvent(blob:http:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:17904:39)       在XMLHttpRequest.setReadyState(blob:http:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:17659:20)       在XMLHttpRequest .__ didCompleteResponse(blob:http:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:17486:16)       在blob:http://:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:17596:47       在RCTDeviceEventEmitter.emit(blob:http:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:3482:37)       在MessageQueue .__ callFunction(blob:http:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:2388:44)       在blob:http://:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:2158:17       在MessageQueue .__ guardSafe(blob:http:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:2350:11)       在MessageQueue.callFunctionReturnFlushedQueue(blob:http:// localhost:8081 / a0fa7e28-6cd7-4abe-899c-cb0e0ec650a8:2157:14)

我使用提取请求的页面的代码段:

import React, {Component} from "react";
import {
    View,
    Text,
    StyleSheet,
    TouchableHighlight,
    ListView,
    Image,
    List,
    FlatList,
} from "react-native";
import {createStackNavigator} from 'react-navigation';

import TurfLijst from './TurfPage';

var rowBackgroundColour = 'white';
var imageBackgroundColour = 'gray';
var nameBackgroundColour = '#ADD8E6'; // light blue
var borderRadius = 5;

class TurfGroupsOverview extends Component {
    constructor() {
        super();

        const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})

        this.state = {
            dataSource: ds,
            user: 'None',
        }
    }

    // fetch turflist data from the database.
    componentWillMount() {
        const URL = 'http://145.**.***.***:9455/group/';
        console.log("Before fetching");

        this.setState({loading: true});
        fetch(URL, {
            method: 'get',
            dataType: 'json',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            }
        }).then(response => response.json())
            .then(response => {
                console.log("after fetching, before saving");

                this.setState({
                    dataSource: this.state.dataSource.cloneWithRows(response),
                });
                console.log("after saving to this.state");
            }).catch(function(error){
            console.log("ERROR OCCURED: " + error);

            throw error;
        });
    }

    renderRow(group) {
        return (
            <TouchableHighlight onPress={() => {
                this.props.navigation.navigate('TurfPage', {
                    group: group,
                    members: group.members,
                    groupName: group.name
                });
            }
            }>
                <View style={[styles.boxContainer, styles.turfList]}>
                    <View style={styles.turfName}>
                        <Text>{group.name}</Text>
                    </View>
                    <View style={styles.turfPicture}>
                        <Image style={{width: "100%", height: "100%", borderRadius: borderRadius}}
                               source={({uri: group.picture})}
                        />
                    </View>
                </View>
            </TouchableHighlight>
        )
    }

    render() {
        return (
            <View style={styles.container}>
                {console.log("Calling the render function")}
                {console.log("dataSource: " + this.state.dataSource.toString())}
                <ListView
                    dataSource={this.state.dataSource}
                    renderRow={this.renderRow.bind(this)}
                />
            </View>
        );
    }
}

export default TurfGroupsOverview;

const styles = StyleSheet.create({
    container: {
        flex: 1,
        flexDirection: 'column',

    },

    boxContainer: {
        height: 100,
        alignItems: 'center',
        justifyContent: 'center',
    },

    turfList: {
        backgroundColor: rowBackgroundColour,
        alignItems: 'center',
        flexDirection: 'row',
    },

    turfListTwo: {
        backgroundColor: 'white',
        flexDirection: 'row',
    },

    turfName: {
        backgroundColor: nameBackgroundColour,
        alignItems: 'center',
        justifyContent: 'center',
        flex: 0.7,
        height: '80%',
        borderRadius: borderRadius,
    },

    turfPicture: {
        backgroundColor: imageBackgroundColour,
        alignItems: 'center',
        justifyContent: 'center',
        flex: 0.2,
        height: '80%',
        maxWidth: 80,
        borderRadius: borderRadius,
    }
});

我尝试获取的数据

[{"uuid":"68e706e1-2e47-43e6-b0b2-2cd91eba107b","name":"Group 1","picture":"http://www.israelgives.org/Pictures/AmutaPictures/5201_Large_%D7%94%D7%99%D7%93%D7%A1%D7%98%D7%90%D7%A8%D7%98%204.jpg","members":[{"uuid":"76f64769-4afa-4216-ac9e-6251da40cf50","firstName":"Ivo","lastName":"Chen","email":"ivo@ivo.nl","imageLocation":"https://thesurrianlife.files.wordpress.com/2011/07/macaque-monkey-canon-dslr-self-portrait-234x300.jpg"},{"uuid":"4d18c8dc-42a0-42fe-9cfc-223af355daaf","firstName":"Martin","lastName":"van Keulen","email":"martin@martin.nl","imageLocation":"https://s.redditmedia.com/t5_93cn9/styles/profileIcon_gsz1rsc9tc401.jpg?fit=crop&crop=faces%2Centropy&arh=1.0&w=256&h=256&s=338416408f027bd8ca437459e99dd146"}],"productList":[{"uuid":"7002648c-fbe4-40b8-9ff7-7aec01cddea1","price":1.0,"amountInStock":10,"imageLocation":null,"nameProcuct":"Hertog Jan","inStock":true},{"uuid":"be6a6b61-3726-401e-8a9c-7883e16338f2","price":1.5,"amountInStock":43,"imageLocation":null,"nameProcuct":"Heineken","inStock":true}]},{"uuid":"69b1a310-2c6d-4ed0-bb51-7cead98382fd","name":"Group 2","picture":"http://www.israelgives.org/Pictures/AmutaPictures/5201_Large_%D7%94%D7%99%D7%93%D7%A1%D7%98%D7%90%D7%A8%D7%98%204.jpg","members":[{"uuid":"feb830b4-14a6-4c40-afc9-2339726b0347","firstName":"Ivo","lastName":"Chen","email":"ivo@ivo.nl","imageLocation":"https://thesurrianlife.files.wordpress.com/2011/07/macaque-monkey-canon-dslr-self-portrait-234x300.jpg"},{"uuid":"e9b43044-6a13-473a-8259-b795215fdbcb","firstName":"Martin","lastName":"van Keulen","email":"martin@martin.nl","imageLocation":"https://s.redditmedia.com/t5_93cn9/styles/profileIcon_gsz1rsc9tc401.jpg?fit=crop&crop=faces%2Centropy&arh=1.0&w=256&h=256&s=338416408f027bd8ca437459e99dd146"}],"productList":[{"uuid":"4e45e620-a18a-4602-93d3-c3a5196a2643","price":1.0,"amountInStock":10,"imageLocation":null,"nameProcuct":"Hertog Jan","inStock":true},{"uuid":"47600a90-7614-422e-a87c-ba0d3c97bb91","price":1.5,"amountInStock":43,"imageLocation":null,"nameProcuct":"Heineken","inStock":true}]},{"uuid":"231a502a-c05f-494e-880e-bfd4b3b2f281","name":"Group 3","picture":"http://ivochen.com/wp-content/uploads/2018/08/Background_No_User.png","members":[{"uuid":"c72348fe-d838-44d4-96b2-4b20d7f68e64","firstName":"Ivo","lastName":"Chen","email":"ivo@ivo.nl","imageLocation":"https://thesurrianlife.files.wordpress.com/2011/07/macaque-monkey-canon-dslr-self-portrait-234x300.jpg"},{"uuid":"c9cf7771-a1f5-41aa-b2d7-c2e887dce2fd","firstName":"Martin","lastName":"van Keulen","email":"martin@martin.nl","imageLocation":"https://s.redditmedia.com/t5_93cn9/styles/profileIcon_gsz1rsc9tc401.jpg?fit=crop&crop=faces%2Centropy&arh=1.0&w=256&h=256&s=338416408f027bd8ca437459e99dd146"}],"productList":[{"uuid":"d206ebc9-2142-4474-9e46-f2aa133684b7","price":1.0,"amountInStock":10,"imageLocation":null,"nameProcuct":"Hertog Jan","inStock":true},{"uuid":"17e329f9-8981-41dc-b474-764bdc3ef871","price":1.5,"amountInStock":43,"imageLocation":null,"nameProcuct":"Heineken","inStock":true}]},{"uuid":"1f39a0c3-8ff2-4fe4-9e81-0480e4c51001","name":"Heren residentie Fjirma Bretels","picture":"http://ivochen.com/wp-content/uploads/2018/08/Background_No_User.png","members":[{"uuid":"95f61185-2536-4204-894c-ae5e49811312","firstName":"Ivo","lastName":"Chen","email":"ivo@ivo.nl","imageLocation":"https://thesurrianlife.files.wordpress.com/2011/07/macaque-monkey-canon-dslr-self-portrait-234x300.jpg"},{"uuid":"b396a5cb-3f30-4f27-82f3-bc102cda38ba","firstName":"Martin","lastName":"van Keulen","email":"martin@martin.nl","imageLocation":"https://s.redditmedia.com/t5_93cn9/styles/profileIcon_gsz1rsc9tc401.jpg?fit=crop&crop=faces%2Centropy&arh=1.0&w=256&h=256&s=338416408f027bd8ca437459e99dd146"}],"productList":[{"uuid":"4746ef90-9c36-45fd-87c1-a2924aa0c792","price":1.0,"amountInStock":10,"imageLocation":null,"nameProcuct":"Hertog Jan","inStock":true},{"uuid":"4059ff8b-09ed-4103-92f1-2590170d7ced","price":1.5,"amountInStock":43,"imageLocation":null,"nameProcuct":"Heineken","inStock":true}]}]

Image for a better view

2 个答案:

答案 0 :(得分:0)

好吧,从我这边傻了。我没有连接相同的WIFI ...

答案 1 :(得分:0)

我通常在什么时候遇到这类问题

  1. 我的互联网无法正常工作。

  2. 我不在部署本地主机的网络上。

  3. 我的仿真器或设备处于飞行模式。