错误改造获取
Erreur Retrofit Get
我无法使用 Retrofit 库从我的 Wordpress API 恢复数据。
错误是:
HTTP 失败:java.net.UnknownHostException:无法解析主机“app.divion.fr”:没有与主机名关联的地址
API 客户端:
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;
}
}
API接口:
package com.example.allomairie.rest;
import java.util.Map;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;
public 接口 ApiInterface {
// Retrofit Interface
//JSON --> GSON Library --> Java Object
@GET("homepage_api")
Call<Object> getHomepageApi(@QueryMap Map<String, String> params);
}
Manifest.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>
谢谢;)
DNS 服务器似乎无法识别您指定的 Url。您确定 https://app.divion.fr/wp-json/api/ 是实际工作的 url 吗?
您可以将 url 复制并粘贴到网络浏览器中,看看它是否会打开一个页面进行验证。如果不是有效的Url,请使用有效的。
我无法使用 Retrofit 库从我的 Wordpress API 恢复数据。 错误是: HTTP 失败:java.net.UnknownHostException:无法解析主机“app.divion.fr”:没有与主机名关联的地址
API 客户端:
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;
}
}
API接口:
package com.example.allomairie.rest;
import java.util.Map;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;
public 接口 ApiInterface {
// Retrofit Interface
//JSON --> GSON Library --> Java Object
@GET("homepage_api")
Call<Object> getHomepageApi(@QueryMap Map<String, String> params);
}
Manifest.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>
谢谢;)
DNS 服务器似乎无法识别您指定的 Url。您确定 https://app.divion.fr/wp-json/api/ 是实际工作的 url 吗? 您可以将 url 复制并粘贴到网络浏览器中,看看它是否会打开一个页面进行验证。如果不是有效的Url,请使用有效的。