Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Studio-无法调用API,因为;指定了网络安全配置,使用平台默认值;错误日志_Android_Android Studio_Retrofit2_Android Debug_Android Network Security Config - Fatal编程技术网

Android Studio-无法调用API,因为;指定了网络安全配置,使用平台默认值;错误日志

Android Studio-无法调用API,因为;指定了网络安全配置,使用平台默认值;错误日志,android,android-studio,retrofit2,android-debug,android-network-security-config,Android,Android Studio,Retrofit2,Android Debug,Android Network Security Config,我正在尝试构建一个转换货币的应用程序。我使用公共API访问货币兑换功能。调试代码时,由于此网络安全错误,我无法查看JSON接收的对象。我在这里查看了其他帖子,并试图通过在AndriodManifest.xml文件中包含安卓:usesCleartextTraffic=“true”和使用权限安卓:name=“android.permission.ACCESS_NETWORK_STATE”来修复它。这是调试输出的图像: 这是我的代码文件 AndriodManifest.xml: <?xml ve

我正在尝试构建一个转换货币的应用程序。我使用公共API访问货币兑换功能。调试代码时,由于此网络安全错误,我无法查看JSON接收的对象。我在这里查看了其他帖子,并试图通过在AndriodManifest.xml文件中包含安卓:usesCleartextTraffic=“true”使用权限安卓:name=“android.permission.ACCESS_NETWORK_STATE”来修复它。这是调试输出的图像:

这是我的代码文件

AndriodManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.currencyconversion">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CurrencyConversion"
        android:usesCleartextTraffic="true"
        >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
www.interface.java:

package com.example.currencyconversion.Retrofit;

import com.google.gson.JsonObject;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface RetrofitInterface {
    @GET("v6/4API-KEY/latest/{currency}")
    Call<JsonObject> getExchangeCurrency(@Path("currency") String currency);
}
package com.example.currencyconversion.reformation;
导入com.google.gson.JsonObject;
2.电话;;
导入文件2.http.GET;
导入2.http.Path;
公共接口改造接口{
@获取(“v6/4API-KEY/latest/{currency}”)
调用getExchangeCurrency(@Path(“currency”)字符串currency);
}
  • 使用clearTextTraffic属性仅影响httprotocol。因此,如果您使用的api的基本url不是通过安全的http协议的,那么。(https)您使用该属性
  • 您是否在android清单文件中声明了使用internet的权限
  • 您可以创建一个network_config.xml文件来设置某些internet配置
  • 您是否查看了正在调用的GET请求的JSON响应主体?层次结构是什么样的,你必须遵循层次结构

    它是一个对象数组,是键、值对所在对象中的对象吗

    你也可以发布你的jsonObject类吗。您是否确保该类中的变量与api中的键具有完全相同的命名约定

    比如,

    如果响应有一个具有此键、值对的对象

    名称:“Eimanalw”

    在jsonObject类中,是否将变量命名为Name而不是Name

    最后,如果您确实更改了字符并将其命名为Name,您是否使用了

    @Serializable(“name”)  //annotation
    String Name;
    

    标题中的消息不是错误。
    package com.example.currencyconversion.Retrofit;
    
    import com.google.gson.JsonObject;
    
    import retrofit2.Call;
    import retrofit2.http.GET;
    import retrofit2.http.Path;
    
    public interface RetrofitInterface {
        @GET("v6/4API-KEY/latest/{currency}")
        Call<JsonObject> getExchangeCurrency(@Path("currency") String currency);
    }
    
    @Serializable(“name”)  //annotation
    String Name;