关于React Native 的网络请求的总结
因为这里使用的是fetch()方法,返回的是一个Promise对象,所以可以使用then()和catch()方法进行链式调用,也可以用all(),race() 去包装多个请求
具体的话可以看这里
GET
1 2 3 4 5 6 7 8 9
   | fetch("http://localhost:3000/get") 	.then((response) => response.json()) // 这里取出响应体的JSON数据并返回 	.then((responseJSON) => { // 处理上面返回的JSON数据 		// do something 		 	}) 	.catch((err) => { // 捕获错误 		// catch err 	});
  | 
 
POST
网上说有两种,不过我一般用第二种比较多
application/json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | var fetchOptions = {            method: 'POST',            headers: {                'Accept': 'application/json',                                'Content-Type': 'application/json'            },            body:JSON.stringify('data=test')         };
  fetch("http://localhost:3000/post", fetchOptions)    .then((response) => response.json())    .then((responseText) => {         console.log(responseText);    });
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   |  var fetchOptions = {            method: 'POST',            headers: {                'Accept': 'application/json',                                'Content-Type': 'application/x-www-form-urlencoded'            },            body:'data=test'         };
  fetch("http://localhost:3000/post", fetchOptions)    .then((response) => response.json())    .then((responseText) => {         console.log(responseText);    });
  | 
 
如果使用的是Restful的API的话,那么只要把上面fetchOption里面的method改成对应的方法就好.
当然,封装一个网络请求工具是有必要的,等我有时间写一下.