一、请求数据

1-1 原生

  1. componentDidMount() {
  2. // ctrl+shift+j
  3. var url = "http://192.168.14.15:5000/dj/program?rid=336355127"
  4. fetch(url).then(res => res.json()).then(resJson => {
  5. console.log(resJson.programs)
  6. this.setState({
  7. movies: resJson.programs
  8. })
  9. })
  10. }

1-2 async await

  1. //支持async await
  2. async componentDidMount() {
  3. // ctrl+shift+j
  4. var url = "http://192.168.14.15:5000/dj/program?rid=336355127"
  5. var data = await fetch(url);
  6. var resJson = await data.json();
  7. console.log(resJson)
  8. }

二、渲染数据

2-1 渲染

  1. import { StyleSheet, Text, View,FlatList ,Image} from 'react-native'; //导入组件
  2. export default class SectionListBasics extends Component {
  3. state={
  4. playlists:[]
  5. }
  6. render() {
  7. return (
  8. <View style={styles.container}>
  9. <FlatList
  10. data={this.state.playlists}
  11. renderItem={({item})=><View>
  12. <Text>{item.name}</Text>
  13. <Image style={{width:100,height:100}}
  14. source={{uri:item.coverImgUrl}}
  15. />
  16. </View>}>
  17. </FlatList>
  18. </View>
  19. );
  20. }

2-2 调试

android studio

  1. 1.ctrl+m:打开调试
  2. 2.点击Debug
  3. 3.ctrl+shift+j //查看页面输入