我无法获得在 android 10 api 29 或 api 30 上工作的 http 请求。不过它确实适用于较低的 api

ian*_*son 1 javascript android json nativescript

我正在使用 Telerik NativeScript 平台编写一个 Android 购物应用程序。我需要获取订单和交付的客户详细信息,因此我让他们在初始启动时进行注册。该应用程序适用于 API 17 到 API 28,我不知道是什么阻止了它在 API 29 和 API 30 上工作。如果我不能让它工作,我将无法将订单发送回网络应用程序工作。编码:-

const Observable = require("tns-core-modules/data/observable").Observable;
const fs = require("tns-core-modules/file-system");
const formObject = require("tns-core-modules/data/observable").fromObject;
const http = require("tns-core-modules/http/");
const Toast = require("nativescript-toast");

const dirPath = fs.knownFolders.documents();
const folder = dirPath.getFolder('sot');

const m = {
 name: "",
 email: "",
 password: "",
 phone: "",
 address: ""
};

const bindingContext = formObject(m);

exports.onLoad = args => {
 const p = args.object;
 p.bindingContext = bindingContext;

 let txtfile = fs.path.join(dirPath.path, 'sot', 'sot.txt');
 let basketFile = fs.path.join(dirPath.path, 'sot', 'basket.txt');
 let exists = fs.File.exists(txtfile);

 if (exists === true) {
  exists = fs.File.exists(basketFile);
  if (exists === true) {
   p.page.frame.navigate('/store/store');
  }
 }

};

exports.tapped = args => {
 const p = args.object;
 const button = args.object;
 const page = button.page;

 let na = p.bindingContext.get('name');
 if (na === "") {
  alert("Please fill in your name");
  return;
 }

 let em = p.bindingContext.get('email');
 if (em === "") {
  alert("Please fill in your email address");
  return;
 }

 let pw = p.bindingContext.get('password');
 if (pw === "") {
  alert("Please supply a password");
  return;
 }

 let ph = p.bindingContext.get('phone');
 if (ph === "") {
  alert("Please fill in your phone number");
  return;
 }

 let ad = p.bindingContext.get('address');
 if (ph === "") {
  alert("Please fill in your address / suberb");
  return;
 }

 let jsonFragment = {
  "na": na,
  "em": em,
  "pw": pw,
  "ph": ph,
  "ad": ad
 };

  console.log('It stops here....');
  console.log(JSON.stringify(jsonFragment));

 http.request({
  url: "http://soapontap.co.za/php/insertMember.php",
  method: "POST",
  headers: {
   "Content-Type": "application/json"
  },

  content: JSON.stringify(jsonFragment)

 }).then(function (response) {

  console.log("done the database thing....");

  setTimeout(function () {
   let file = folder.getFile('sot.txt');
   let dataToWrite = JSON.stringify(jsonFragment);

   file.writeText(dataToWrite).
    then(function () {

     file = folder.getFile('basket.txt');
     file.writeText("")
      .then(() => {
       let toast = Toast.makeText("Welcome");
       toast.show();
       page.frame.navigate('/store/store');
      }, function (error) {
       console.log("Could not write to basket.txt");
      });

    }, function (error) {
     alert({
      title: "Error",
      message: 'Could not record the user on this local device.',
      okButtonText: "Close"
     });
    });

  }, function (e) {
   let toast = Toast.makeText("Error occurred " + e);
   toast.show();
  });
 }, 3000);

};
Run Code Online (Sandbox Code Playgroud)

我已设置权限,这是清单:-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="__PACKAGE__" android:versionCode="6" android:versionName="1.0.220720.1916">
 <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true"/>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <application android:name="com.tns.NativeScriptApplication" android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme">
  <activity android:name="com.tns.NativeScriptActivity" android:label="@string/title_activity_kimera" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode" android:theme="@style/LaunchScreenTheme" android:screenOrientation="unspecified">
   <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme"/>
   <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
   </intent-filter>
  </activity>
  <activity android:name="com.tns.ErrorReportActivity"/>
 </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

我已在设备上授予应用程序权限。任何帮助,将不胜感激。谢谢。

Kar*_*wal 6

问题在于您的 API 网址。

它的http://. 由于 Android 10 不允许您访问使用https://.

现在您有 2 个解决方案来克服这个问题:

  1. 在您的 Web 服务器域上安装 SSL 以使其成为 https://(推荐)
  2. android:usesCleartextTraffic="true"在您的清单中添加<application>标签

注意:如果您选择第二个选项并将您签名的 APK 上传到 Play 商店,它可能会因安全问题而被拒绝。