首页 > 解决方案 > Erreur 改造 获取

问题描述

我无法使用 Retrofit 库从我的 Wordpress API 中恢复数据。错误是:HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "app.divion.fr": No address associated with hostname

接口客户端:

import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import kotlin.contracts.Returns;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient extends AppCompatActivity {

//Adresse de l'Api

public static final String BASE_URL = "https://app.divion.fr/wp-json/api/";

//Retrofit Api Client

private static Retrofit retrofit = null;

public static Retrofit getApiClient(){
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
                                .addInterceptor(loggingInterceptor)
                                .build();
    if (retrofit == null){

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

}

接口接口:

package com.example.allomairie.rest;

import java.util.Map;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;

公共接口 ApiInterface {

// Retrofit Interface
//JSON --> GSON Library --> Java Object


@GET("homepage_api")
Call<Object> getHomepageApi(@QueryMap Map<String, String> params);

}

清单.xml

    <?xml version="1.0" encoding="utf-8"?>

<!-- Ajout de la permission internet pour les actualités worpdress -->
<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- if you want to load images from a file OR from the internet -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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.AlloMairie">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

谢谢 ;)

标签: androidretrofitokhttphostname

解决方案


看起来 DNS 服务器无法识别您指定的 URL。你确定https://app.divion.fr/wp-json/api/是一个实际的工作网址吗?您可以将 url 复制并粘贴到网络浏览器中,看看它是否会打开一个页面进行验证。如果它不是一个有效的 URL,请使用一个有效的 URL。


推荐阅读