skip to Main Content

i am building a react native app, through rest-api in django framework, in test mode or in expo go the code is working fine but in, but after the apk build , data is not fetching

const fetchDataFromAPI = async (code) => {
    setLoading(true);
    setError(null);
    if (ipAddress && profileCode) {
      try {
        const apiUrl = `http://${ipAddress}:8000/api/stock/?i=${profileCode}&s=${code}`;
        setError(apiUrl);
        const response = await Axios.get(apiUrl);
        const stockData = response.data;
        setData(stockData);
      } catch (error) {
        setError('error in fetching');
        setData();
      } finally {
        setLoading(false);
      }
    } else {
      setError('Ip Address Not Found');
    }
  };

after build always getting error in fetching
the total code in git repo is in following

https://github.com/pruthviraj-vegi/Ssc-React-Native

2

Answers


  1. Ensure that your app has the necessary network permissions. Add the following permission to your AndroidManifest.xml file within the <manifest> tag:

    <uses-permission android:name="android.permission.INTERNET" />
    

    Also verfify the Cross-Origin Resource Sharing (CORS) policy on your Django server.

    Install the library

    python -m pip install django-cors-headers
    

    and then add it to your installed apps:

    INSTALLED_APPS = (
        ...
        'corsheaders',
        ...
    )
    

    You will also need to add a middleware class to listen in on responses:

    MIDDLEWARE = [
        ...,
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.common.CommonMiddleware',
        ...,
    ]
    

    and specify domains for CORS, e.g.:

    CORS_ALLOWED_ORIGINS = [
        'http://localhost:3030',
    ]
    

    Refer the following answer for configuring CORS in django.

    https://stackoverflow.com/a/35761088/7103882

    Login or Signup to reply.
  2. the problem is this url: http://${ipAddress}:8000/

    Please read this link and it will help you https://stackoverflow.com/a/51892067/1308590

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search