zoukankan      html  css  js  c++  java
  • React Native网络请求

    发环境 Atom、phpStudy2018、Genymotion (Window10)

     

    一、环境配置

    1.1安装Reactnative.

    https://reactnative.cn/docs/0.51/getting-started.html#content

    1.2Android模拟器Genymotion安装

    https://www.cnblogs.com/whycxb/p/6850454.html

     

    二、代码

    2.1客户端

    2.11App.js

    import React, { Component } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View
    } from 'react-native';
    var GetData = require("./getData");
    
    export default class AwesomeProject extends Component {
      render() {
        return (
          <GetData></GetData>
        );
      }
    }

    2.12getData.js

    import React, {
      Component
    } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      TouchableOpacity
    } from 'react-native';
    
    var getUrl = "http://47.104.102.226/public/ajax/admin.php";
    var postUrl = "http://192.168.1.177/react/demo.php/";
    
    class getData extends Component{
        getRequest(url){
            var opts = {
                method:"GET"
            }
            fetch(url,opts)
                .then((response) => {
                    return response.text();
                })
                .then((responseText) => {
                    alert(responseText);
                })
                .catch((error) =>{
                    alert(error);
                })
        }
        postRequest(url){
          var opts = {
            method: 'POST',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
              username: 'userValue',
              password: 'passValue',
            })
          }
    
          fetch(url,opts)
              .then((response) => {
                  return response.text();
              })
              .then((responseText) => {
                  alert(responseText);
              })
              .catch((error) => {
                  alert(error)
              })
        }
        render(){
            return(
                <View style={styles.container}>
                    <TouchableOpacity onPress={this.getRequest.bind(this,getUrl)}>
                        <View style={styles.btn}>
                            <Text>GET</Text>
                        </View>
                    </TouchableOpacity>
                    <TouchableOpacity onPress={this.postRequest.bind(this,postUrl)}>
                        <View style={styles.btn}>
                            <Text>POST</Text>
                        </View>
                    </TouchableOpacity>
                </View>
            );
        }
    }
    const styles = StyleSheet.create({
        container:{
            backgroundColor:"cyan",
            flexDirection:"row",
            justifyContent:"space-around",
            alignItems:"center",
            flex:1
        },
        btn:{
            60,
            height:30,
            borderWidth:1,
            borderRadius:3,
            borderColor:"black",
            backgroundColor:"yellow",
            justifyContent:"center",
            alignItems:"center"
    
        }    
    });
    module.exports = getData;

    2.2服务器端

    C:phpStudyPHPTutorialWWW eactdemo.php

    <?php
            
        $json = json_decode(file_get_contents('php://input'), true);
        
        $username = $json['username'];
        $password = $json['password'];
    
        $response = array("success" => true, "username" => $username, "password" => $password);
    
        echo json_encode($response);
    ?>

    三、运行

    3.1运行结果

    3.2Atom工程结构

     

  • 相关阅读:
    LRT最大似然比检验
    EPNP理论分析
    奇异值SVD分解
    矩阵求导
    static_cast和dynamic_cast用法
    Django 使用 Celery 实现异步任务
    python爬虫实战一:分析豆瓣中最新电影的影评
    scrapy模拟登陆知乎--抓取热点话题
    一个小时搭建一个全栈Web应用框架(上)
    一个小时搭建一个全栈 Web 应用框架(下)——美化与功能
  • 原文地址:https://www.cnblogs.com/JimmyCode/p/8488122.html
Copyright © 2011-2022 走看看