Android设备使用cordova-plugin-advanced-HTTP报错cordova_not_available求助
Hey there, let's dig into this sudden issue—since your HTTP GET requests were working fine just two days ago, this is almost certainly a build, configuration, or environment glitch rather than a problem with your core code. Let's walk through the most common fixes step by step:
1. Verify Cordova is loaded and you're in the right environment
First, let's address the direct error message you shared:
[20:35:26] console.warn: Native: tried calling HTTP.get, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
[20:35:26] console.log: cordova_not_available
- Don't test in a regular desktop browser: Cordova plugins rely on native platform APIs, so running your app directly in Chrome/Firefox won't work. You must use
cordova run android,cordova run ios, or launch a simulator/emulator to test native plugin functionality. - Check for cordova.js in your index.html: The script
<script src="cordova.js"></script>should be present at the bottom of your<body>tag. Note: Cordova injects this automatically at build time—don't add a local file manually. If it's missing, a corrupted build might be the culprit (we'll fix that next).
2. Clean and rebuild your Cordova project
Corrupted build artifacts are a super common cause of sudden failures. Follow these steps to reset your build:
- Remove the target platform (replace
androidwith your actual platform if needed):cordova platform remove android - Clear Cordova's build cache:
cordova clean - Re-add the platform:
cordova platform add android - Rebuild and run the app:
cordova build android && cordova run android
3. Ensure the plugin is properly installed
It's possible the plugin got unlinked or corrupted during a recent change:
- Check if the plugin is listed in your project:
cordova plugin list - If
cordova-plugin-advanced-HTTPisn't in the list, reinstall it:cordova plugin add cordova-plugin-advanced-HTTP - Rebuild your project right after reinstalling to ensure the plugin integrates correctly.
4. Wait for Cordova's deviceready event before calling the plugin
Cordova plugins can only be used after the deviceready event fires. If your code is calling HTTP.get before this event, you'll get the "not available" error—even if it worked before (sometimes initialization timing shifts with new builds).
In your Angular component, wrap your HTTP call in a listener for this event. If you're using Ionic, you can use the Platform.ready() method for simplicity:
import { Component, OnInit } from '@angular/core'; import HTTP from '@ionic-native/http/ngx'; import { Platform } from '@ionic/angular'; @Component({ selector: 'app-your-component', templateUrl: './your-component.component.html', styleUrls: ['./your-component.component.css'] }) export class YourComponent implements OnInit { constructor(private http: HTTP, private platform: Platform) { } ngOnInit(): void { // Wait for Cordova to be fully initialized this.platform.ready().then(() => { // Now make your GET request this.http.get('https://your-api-url.com', {}, {}) .then(response => { console.log('Response:', response); }) .catch(error => { console.error('Error:', error); }); }); // If you're not using Ionic, use the native deviceready event directly: // document.addEventListener('deviceready', () => { // // Your HTTP call here // }, false); } }
5. Check for recent environment changes
Think back to what changed yesterday:
- Did you update Cordova, Node.js, or npm packages? Try rolling back to the versions you used when the app worked.
- Did you modify
config.xml? Double-check for any changes that might disable Cordova's native integration. - Did you switch development machines? Ensure all dependencies (SDKs, Cordova CLI) are installed correctly on the new machine.
内容的提问来源于stack exchange,提问作者kamal951

