简体   繁体   English

将 api 连接到 react-native 时出现问题

[英]Problems connecting api to react-native

i was doing a new app with react native i'm using rails/graphql but the problem is when i try to call the api url in my app with the configuration that i've made.我正在使用rails / graphql做一个带有react native的新应用程序,但问题是当我尝试使用我所做的配置在我的应用程序中调用api url时。 it doesnt work at all (lauching expo cli), the error that o get is:它根本不起作用(启动 expo cli),得到的错误是:

[Error: Network error: Network request failed] [错误:网络错误:网络请求失败]

i've already set the ip address of my pc instead of 'localhost' but i dont get any results my code is:我已经设置了我的电脑的 ip 地址而不是“本地主机”,但我没有得到任何结果我的代码是:

// React
import React from 'react';

// React-native
import { StyleSheet, Text, View } from 'react-native';

// Components
import Test from './app/components/Test/index';

import { ApolloProvider } from 'react-apollo'
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { setContext } from 'apollo-link-context';

// 2
const httpLink = createHttpLink({
  uri: 'http://my-ip-address:3000/graphql'
})

// 3
const client = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache()
})

export default class App extends React.Component {
  render() {
    console.log('client ', client)
    return (
      <ApolloProvider client={client}>
        <Test />
      </ApolloProvider> 
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

the component that is calling the query is:调用查询的组件是:

// React
import React, { Fragment } from 'react';

// React-native
import { StyleSheet, Text, View } from 'react-native';

// React Apollo
import { graphql } from 'react-apollo';
import {flowRight as compose} from 'lodash';

 // Queries
 import queries from './queries';

 class Test extends React.Component {
   constructor() {
     super();
   }

   render() {
     console.log('props ', this.props);
     return(
       <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
         <Text>
           Se conecto! 🎉
         </Text>
       </View>
     );
   }
 }


 export default compose(
   graphql(queries.getSpots, { name: 'getSpots' }),
 )(Test);

everything with the configuration was fine, the only thing that was happening was the way to start the rails server with my current version of rails.配置的一切都很好,唯一发生的事情是使用我当前版本的 rails 启动 rails 服务器的方式。 so instead of start off rails server with "rails s" i had to use "rails -b 0.0.0.0".所以我不得不使用“rails -b 0.0.0.0”来代替使用“rails s”启动rails服务器。 after that i could connect my app from android emulator to my localhost:3000/graphiql之后我可以将我的应用程序从 android 模拟器连接到我的 localhost:3000/graphiql

I didn't read your code but I know problem: go to infor.plist我没有阅读您的代码,但我知道问题:go to infor.plist

add (or change key) to react native - ios allow your api url添加(或更改密钥)以响应本机 - ios 允许您的 api url

<key>NSAppTransportSecurity</key>
          <dict>
              <key>NSExceptionDomains</key>
              <dict>
                  <key>Your-API-URL-ROOT-HERE</key>
                  <dict>
                      <!--Include to allow subdomains-->
                      <key>NSIncludesSubdomains</key>
                      <true />
                      <!--Include to allow HTTP requests-->
                      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                      <true />
                      <!--Include to specify minimum TLS version-->
                      <key>NSTemporaryExceptionMinimumTLSVersion</key>
                      <string>TLSv1.1</string>
                  </dict>
              </dict>
          </dict>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM