• React Native 网络请求


    如下面的Code,分别介绍了GET,POST,以及使用XMLHttpRequest的Get请求。

    import React, { Component } from 'react';
    import { AppRegistry,
     StyleSheet,
     Text,
     View,
     Image,
     TouchableOpacity,
     TouchableHighlight,
     ToastAndroid,
     Alert, } from 'react-native';
    
    var BASE_URL = 'https://m.baidu.com';
    
    class AlignItemsBasics extends Component {
    
        getEvent() {
            fetch('https://m.baidu.com' )
                .then((response) => response.text())
                .then((responseText) => {
                  ToastAndroid.show(responseText, ToastAndroid.SHORT);
                  console.warn(new Date().getMilliseconds());
                })
                .catch((error) => {
                  console.warn(error);
                }).done();
    
        }
    
        getByXMLHttpRequest(){
            var request = new XMLHttpRequest();
            request.onreadystatechange = (e) => {
                if(request.readyState !== 4){
                    return;
                }
                if(request.status === 200){
                    ToastAndroid.show('success' + request.responseText, ToastAndroid.SHORT);
                }else{
                    ToastAndroid.show('error', ToastAndroid.SHORT);
                }
            };
            request.open('GET','http://xxx/xxx');
            request.send();
        }
    
         postSource(){
            fetch('https://m.baidu.com' ) //
            .then((response) => response.text())
            .then((responseText) => {
                // Works on both iOS and Android
                Alert.alert(
                  '请求结果',
                  responseText.substring(0,100),
                  [
                    {text: 'Ask me later', onPress: () => console.warn('Ask me later pressed')},
                    {text: 'Cancel', onPress: () => console.warn('Cancel Pressed'), style: 'cancel'},
                    {text: 'OK', onPress: () => console.warn('OK Pressed')},
                  ]
                )
            })
            .catch((error) => {
              console.warn(error);
            }).done();
          }
    
        render() {
    
          return (
            <View style={{flex:1, justifyContent:'center', alignItems:'center'}}>
                <TouchableHighlight  onPress={this.getEvent} style={styles.button}>
                      <Text>Get 请求</Text>
                </TouchableHighlight>
                 <TouchableHighlight  onPress={this.getByXMLHttpRequest} style={styles.button}>
                      <Text>使用XMLHttpRequest Get 请求</Text>
                 </TouchableHighlight>
                 <TouchableHighlight  onPress={this.postSource} style={styles.button}>
                      <Text>Post 请求</Text>
                 </TouchableHighlight>
            </View>
    
          );
        }
    
    
    };
    
        var styles = StyleSheet.create({
            button: {
              width : 180,
              height: 50,
              justifyContent:'center',
              backgroundColor: '#e2e2e2',
              alignItems:'center',
              margin: 10,
            }
        });
    
    AppRegistry.registerComponent('AwesomeProject', () => AlignItemsBasics);
    

      

  • 相关阅读:
    转:10+年程序员总结的20+条经验教训
    年损失超20亿,手游行业第三方安全服务需求迫切
    分享:Android 应用有哪些常见,浅谈常被利用的安全漏洞?
    Android手机开发(一)
    分享:不懂技术,不要对懂技术的人说这很容易实现
    spring-boot学习六:外部配置加载顺序
    spring-boot学习五:Spring boot配置文件的加载位置
    spring-boot学习一:使用Spring Initializr快速创建Spring boot项目
    数值比较有说头
    常见SQL积累
  • 原文地址:https://www.cnblogs.com/linlf03/p/5954160.html
Copyright © 2020-2023  润新知