面对 java.net.UnknownHostException:无法解析主机“tfg.test”:没有与主机名关联的地址

问题描述 投票:0回答:0

m trying to use retrofit with android but I
正面临这个问题,不知道如何解决。我曾尝试在具有互联网连接的物理设备上执行此操作,但它不起作用。这是我的文件: 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.retrofitpruebabuena">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.RetrofitPruebaBuena"
        tools:targetApi="31"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

API:

package com.example.retrofitpruebabuena.API;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class API {

    public static final String BASE_URL="http://tfg.test/";
    private static Retrofit retrofit = null;

    public static Retrofit getApi(){
        if (retrofit==null){
            retrofit=new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }


}

主要:

package com.example.retrofitpruebabuena;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

import com.example.retrofitpruebabuena.API.API;
import com.example.retrofitpruebabuena.API.ApiService;
import com.example.retrofitpruebabuena.models.Alumno;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ApiService service= API.getApi().create(ApiService.class);
        Call<List<Alumno>> alumnosCall=service.getStudents(25);

        alumnosCall.enqueue(new Callback<List<Alumno>>() {
            @Override
            public void onResponse(Call<List<Alumno>> call, Response<List<Alumno>> response) {
                if(response.isSuccessful()){
                    List<Alumno> alumnos=response.body();
                    Toast.makeText(MainActivity.this, "El servidor retornó datos", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "El servidor retornó un error", Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onFailure(Call<List<Alumno>> call, Throwable t) {
                if(t instanceof IOException){
                    Toast.makeText(MainActivity.this, "Error de conexión", Toast.LENGTH_SHORT).show();
                    t.printStackTrace();
                }else{
                    Toast.makeText(MainActivity.this, "Problema de conversión", Toast.LENGTH_SHORT).show();
                }



            }
        });


    }
}

API服务:

package com.example.retrofitpruebabuena.API;

import com.example.retrofitpruebabuena.models.Alumno;

import java.util.List;

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

public interface ApiService {
    @GET("api/courses/{course}")
    Call<List<Alumno>> getStudents(@Path("course") int course);
}

我尝试使用物理设备在 manfinest 文件中包含互联网许可,但它们都不起作用

android retrofit
© www.soinside.com 2019 - 2024. All rights reserved.