Axios doesn't work with Android (emulator) raising a Network Error
See original GitHub issueI’n new with React Native, and I was playing around with components and http ajax request, and all went fine with iOs emulator, until I thought to test on Android emulator.
This is my Http class:
import 'url-search-params-polyfill';
import axios from 'axios';
import { AsyncStorage } from 'react-native';
import Rx from 'rxjs/Rx';
let _HttpServiceInstance = null;
class HttpService {
constructor(envStr) {
console.log('DEFAULT', axios.defaults);
if(!_HttpServiceInstance){
this.__proto__.ghtnk = null;
axios.defaults.baseURL = this.getBaseUrlApi()[envStr || 'DEV'];
axios.interceptors.response.use(
(resp) => {
let ghresp = resp.data || resp;
ghresp.status = resp.data ? resp.data.status : resp.status;
return ghresp;
},
(err) => {
if( err.status === 401 ){
this.removeToken();
// go to login
}
return Promise.reject(err);
}
)
_HttpServiceInstance = this;
}
return _HttpServiceInstance;
}
getBaseUrlApi = () => {
return {
DEV : 'https://dev-api.domainname.com/',
TEST: 'https://test-api.domainname.com/',
QA : 'https://qa-api.domainname.com/',
LIVE: 'https://api.domainname.com/',
};
}
switchBaseUrlApi(envStr) {
axios.defaults.baseURL = this.getBaseUrlApi()[envStr];
}
getToken = (callback) => {
let promise = AsyncStorage.getItem('ghtkn');
if( callback ){
promise.then(callback, () => { console.log('getToken: ', err) });
}
else return promise;
};
setToken = (token) => {
return AsyncStorage.setItem('ghtkn', token);
};
removeToken = () => {
AsyncStorage.removeItem('ghtkn');
};
setAuthHeaders = (callback, noNeedToken) => {
if ( noNeedToken ) {
callback(); return;
}
this.getToken(
(token) => {
if (token) {
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
callback();
}
else{ console.log('NO TOKEN PRESENT'); }
},
(err) => { console.log('GET TOKEN: ', err); },
);
};
// GET
get = (url) => {
let subject = new Rx.Subject();
this.setAuthHeaders((token) => {
axios.get(url).then(
(data) => { subject.next(data); },
(err) => { console.log('GET: ', err); }
);
});
return subject;
};
// POST
post = (url, payload, noNeedToken) => {
let subject = new Rx.Subject();
this.setAuthHeaders(() => {
axios.post( url, this.setParams(payload)).then(
(resp) => { subject.next(resp); },
(err) => {
console.log('POST ERROR');
console.log(err.request, Object.keys(err) );
subject.error(err);
}
);
}, noNeedToken);
return subject;
};
// PUT
put = (url, payload) => {
let subject = new Rx.Subject();
this.setAuthHeaders((token) => {
axios.put( url, this.setParams(payload)).then(
(resp) => { subject.next(resp); },
(err) => { console.log('PUT: ', err); }
);
});
return subject;
};
// LOGIN
login = (user, pw) => {
return this.post('user/authentication', {username: user, password: pw}, true)
.do((resp) => { this.setToken(resp.token); })
};
setParams = (jsonParams) => {
let params = new URLSearchParams();
for(param in jsonParams){
if( jsonParams.hasOwnProperty(param) ){
params.append(param, jsonParams[param]);
}
}
return params;
};
}
export { HttpService };
This is the error I’m getting from the post request, using login() function I created using axios.post()
NOTE:
-
I’m using the latest version of Axios ^0.16.2
-
My AndroidManifest.xml contains:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
- The APIs are under VPN, as I wrote all works fine with iOs emulator but doesn’t with the Android one.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:32
- Comments:58
Top Results From Across the Web
Network Error when use axios in React Native - Stack Overflow
Sometimes, it's not working even with android emulator. But after deleted original emulator and created new emulator, it's working. const ...
Read more >Axios doesn't work with Android (emulator) raising a Network ...
I'n new with React Native, and I was playing around with components and http ajax request, and all went fine with iOs emulator, ......
Read more >Axios gives Network Error on android 6 : r/reactnative - Reddit
So I'm getting an error while making a API call using Axios. I have multiple scenarios: If I use https then it won't...
Read more >Using Axios with React Native to manage API requests
Learn how to supercharge your API network requests in React Native with Axios in this comprehensive tutorial.
Read more >Api requests do not work on Android ( Network Error )
SDK Version: 40.0.0 Platforms: Android Api requests do not work on Android, I used axios and fetch for api requests. I'm getting an...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you are trying to call localhost on android simulator created with AVD, replacing
localhost
with10.0.2.2
solved the issue for me.will work in iOS but will not work in Android. HTTPS doesn’t support in android.