使用react-native的connetapi
我的API有问题。我想知道react native的代码结构,因为我无法使用此值转换数据 我没有尝试使用此代码:使用react-native的connetapi,api,react-native,Api,React Native,我的API有问题。我想知道react native的代码结构,因为我无法使用此值转换数据 我没有尝试使用此代码: var data = { liveness:1, hp: '085156426760', email: 'stepheen.jonathaan@gmail.com', nama_lengkap: 'Stephen Jonathan Gustav',
var data = {
liveness:1,
hp: '085156426760',
email: 'stepheen.jonathaan@gmail.com',
nama_lengkap: 'Stephen Jonathan Gustav',
jumlah: 1500000,
komentar: 'test',
hubungan_dengan_peminjam: 'orangtua',
jangka_waktu: '15 hari',
id_warung: '123ab',
nama_pt: 'dbi'
}
这是照片
许多移动应用程序需要从远程URL加载资源。您可能希望向RESTAPI发出POST请求,或者可能需要从另一台服务器获取一块静态内容 React Native为您的网络需求提供获取API。如果您以前使用过XMLHttpRequest或其他网络API,则Fetch似乎很熟悉 提出请求 为了从任意URL获取内容,您可以传递URL以获取:
fetch('Your URL to fetch data from');
Fetch还接受一个可选的第二个参数,该参数允许您自定义HTTP请求。您可能需要指定其他标题,或发出POST请求:
fetch("Your URL to fetch data from", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
firstParam: "yourValue",
secondParam: "yourOtherValue"
})
});
联网本质上是一种异步操作。Fetch方法将返回一个承诺,这使得编写以异步方式工作的代码变得简单:
function getMoviesFromApiAsync() {
return fetch("Your URL to fetch data from")
.then(response => response.json())
.then(responseJson => {
return responseJson.movies;
})
.catch(error => {
console.error(error);
});
}
您还可以在React本机应用程序中使用建议的ES2017 async/await语法:
async function getMoviesFromApi() {
try {
let response = await fetch("Your URL to fetch data from");
let responseJson = await response.json();
console.log(responseJSon);
return responseJson;
} catch (error) {
console.error(error);
}
}
API调用的另一个方法是。Axios是一款非常流行的HTTP客户端(Github上有52k多个星星),它允许我们从浏览器发出GET和POST请求
示例完整代码库
import React from 'react';
import { FlatList, ActivityIndicator, Text, View } from 'react-native';
export default class FetchExample extends React.Component {
constructor(props){
super(props);
this.state ={ isLoading: true}
}
componentDidMount(){
return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson.movies,
}, function(){
});
})
.catch((error) =>{
console.error(error);
});
}
render(){
if(this.state.isLoading){
return(
<View style={{flex: 1, padding: 20}}>
<ActivityIndicator/>
</View>
)
}
return(
<View style={{flex: 1, paddingTop:20}}>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <Text>{item.title}, {item.releaseYear}</Text>}
keyExtractor={({id}, index) => id}
/>
</View>
);
}
}
从“React”导入React;
从“react native”导入{FlatList,ActivityIndicator,Text,View};
导出默认类FetchExample扩展React.Component{
建造师(道具){
超级(道具);
this.state={isLoading:true}
}
componentDidMount(){
返回获取('https://facebook.github.io/react-native/movies.json')
.then((response)=>response.json())
.然后((responseJson)=>{
这是我的国家({
孤岛加载:false,
数据来源:responseJson.movies,
},函数(){
});
})
.catch((错误)=>{
控制台错误(error);
});
}
render(){
if(此.state.isLoading){
返回(
Axious是一款基于promise的http客户端,用于浏览器和NodeJ。Axious可以轻松地向REST端点发送异步http请求并执行CRUD操作
const data = new FormData();
data.append("foto_selfie_kttp", {
uri: response.uri,
type: response.type,
name: response.fileName});
data.append("foto_ktp", {
uri: response.uri,
type: response.type,
name: response.fileName});
data.append(
"inputString",
JSON.stringify({
{
liveness:1,
hp: '085156426760',
email: 'stepheen.jonathaan@gmail.com',
nama_lengkap: 'Stephen Jonathan Gustav',
jumlah: 1500000,
komentar: 'test',
hubungan_dengan_peminjam: 'orangtua',
jangka_waktu: '15 hari',
id_warung: '123ab',
nama_pt: 'dbi'
}
})
);
const URL = FILE_UPLOAD_URL;
return axios(URL, {
method: "POST",
headers: {
"Content-Type": "multipart/form-data",
Authorization: "Basic shyhbG9tOnNoYWxvsadAMTIz"
},
data: data
})
.then(res => {
if (res.data.success === true) {
console.log(res);
}
})
.catch(error => {
console.log(error);
return error;
});
为您的问题添加更清晰的信息..您想知道如何发出API请求并获得API响应吗?是的,请@AkilaDevinda