当前位置: 首页 > 知识库问答 >
问题:

React本机获取请求失败

辛建业
2023-03-14

我不知道我改变了什么,但是获取请求不再工作了(只使用Android)。它以前工作过,试图改变它回来,但不能让它工作。

我尝试了本期中的链接:React本地网络请求在获取数据时失败

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}]}]

图像以便更好地查看

共有1个答案

谭俊
2023-03-14

Oke把傻瓜从我身边戒掉。我没有连接同一个WiFi...

 类似资料:
  • 这段代码在几个月内运行良好: 但上周,react native抛出了一个错误“网络请求失败”。我确信我们的API正在工作,因为我已经在一些在线API测试仪上测试过了。 当我没有强制使用HTTPS,只有HTTP时,它就工作了。 我们当前的反应版本是:0.41 以下是错误:

  • 问题内容: 我有一个应用程序,它使用获取来验证用户身份。直到几天前它一直在工作,我什么都没改变。刚刚从React 0.27升级到0.28,未获取无法正常工作。 我已经搜索了将近2天,并且我已经阅读了stackoverflow中几乎所有的问题。大多数尝试从localhost获取内容的用户,当他们将其更改为实际ip地址时,便可以使用它。但是我没有从本地主机获取任何东西,我的代码也一直在工作。 这是我的

  • 问题内容: 我正在使用React Native开发一个简单的应用程序。我正在Android设备上对其进行测试。我创建了一个Node.js服务器来监听请求,该服务器运行在http:// localhost:3333 /上 。接下来,我要从index.android.js进行提取请求。下面是代码。 节点服务器上请求处理程序的代码如下 但是,获取请求无效。我在Chrome控制台中收到的错误是:TypeE

  • 我试图建立一个基本的网页,允许用户在数据库的mysql表中添加一行,以便打开LED灯和使用arduino。然而,当我尝试提交表单时,页面返回500状态,mysql查询没有执行。我在我的电脑上运行linux-apache-mysql-php服务器。为什么会这样?

  • 我面临一个非常奇怪的问题: 我尝试获取一个反应本机应用程序。在物理iPhone一切正常,在模拟器上我得到错误“网络请求失败”。 API是https,我已经更改了信息。plist允许NSAllowsArbitraryLoads。 我nfo.plist: 我建议是证书之类的。

  • 问题内容: 我可以使用Groovy脚本获取响应xml。我需要获取请求XML,因为我需要在soap ui测试中添加“断言脚本”。 我正在使用以下代码来获取响应xml 但是我不确定如何获取SOAPUI的请求xml。谁能帮我获得SOPAUI的请求xml吗? 问题答案: 要以字符串形式获取请求内容,可以使用 有关SoapUI API的更多信息,请访问http://www.soapui.org/apidoc