【发布时间】:2019-03-19 22:21:57
【问题描述】:
我不知道我改变了什么,但是获取请求不再起作用(仅使用 Android)。以前可以用,想改回来,还是不行。
我试过这个问题的链接:React native network request failed while fetching data
我使用了正确的 IP 地址。 (以 Ipconfig IPv4 地址作为 URL),就像我一直做的那样。 android 调试服务器主机和设备端口上的开发设置为空,设备通过 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)
我使用获取请求的页面的代码 sn-p:
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}]}]
【问题讨论】:
-
好吧,我没注意我不在同一个 WIFI 上。不必更改任何代码...
标签: android react-native request fetch