码迷,mamicode.com
首页 > 其他好文 > 详细

React Native网络请求

时间:2018-03-01 17:21:17      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:技术   man   dmi   exp   password   body   registry   items   erro   

发环境 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:{
        width:60,
        height:30,
        borderWidth:1,
        borderRadius:3,
        borderColor:"black",
        backgroundColor:"yellow",
        justifyContent:"center",
        alignItems:"center"

    }    
});
module.exports = getData;

 

2.2服务器端

C:\phpStudy\PHPTutorial\WWW\react\demo.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工程结构

技术分享图片

 

 

React Native网络请求

标签:技术   man   dmi   exp   password   body   registry   items   erro   

原文地址:https://www.cnblogs.com/JimmyCode/p/8488122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!