Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

https connection in Android react-native not working on physical device after build

I created a simple app with react-native, using expo & Axios (for server requests)

Before build, while developing, all https requests worked fine.

After build, when running the apk on a physical device, https is not working at all.

The error I get with Logcat is "Network Error". Other Internet connections (after build) in the app do work, like webview opening a web page or Firebase connections also.

    analyzerApi.post('/analyze', urls) .then((res) => { 
dispatch({type: 'get_result', payload: res.data.analysis})}).catch(err => console.log("Error in getting analyze.. " ,err.name, err.message))

(analyzerApi is an axios instance with baseUrl directed to my server)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This call will work for both HTTP and HTTPS Try this example for POST CALL

fetch('http://mywebsite.com/endpoint/', {
      method: "POST",
      headers: {
        // Authorization: "Bearer " + Token,
        Accept: 'application/json',
      'Content-Type': 'application/json'
      },
      body: JSON.stringify({
       // sending data userID username, password, etc
      }),
    })
      .then((response) => response.json())
      .then((responseJson) => {
       // Response will here
      })
      .catch((error) => {
        alert(error);
      });

Try this example for GET CALL

fetch('http://mywebsite.com/endpoint/', {
      method: "GET",
      headers: {
       // Authorization: "Bearer " + Token,
       // OR USER name PASSworrd
       Accept: 'application/json',
      'Content-Type': 'application/json'
      },
    })
      .then((response) => response.json())
      .then((responseJson) => {

        // Response will here
      })
      .catch((error) => {
        alert(error);
      });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...