本文介绍了(Slim + Retrofit Android Studio App)请求不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Slim Framework 和 Retrofit Library 开发一个 android 应用程序以将 de API 连接到应用程序,但是我遇到了一个问题,当我在我的电脑上工作并使用 Android Studio 模拟器尝试应用程序时,一切正常很好,但是当我使用具有相同 API 和相同 Android Studio 项目的笔记本电脑时,请求似乎不起作用,我的意思是,当我按下按钮登录应用程序时,没有执行请求或请求不起作用,我不知道为什么.

I’m developing an android app using Slim Framework and Retrofit Library to connect de API to the app, but I have a problem, when I’m working in my pc and I try the app using the Android Studio emulator everything works fine, but when I use my laptop with the same API and the same Android Studio project, it seems that the requests aren’t workin, I mean, when I press a button to login the app doesn’t do the request or the request doesn’t work and I dont know why.

谁能知道如何解决它?

非常感谢

那是我用来连接 android 应用程序和 slim API 的 RetrofitClient 类,BASE_URL 是我的 PC IP,BASE_URL_PC 是我的笔记本电脑 IP,每次我更改网络连接时,我都会使用 cmd 和 ipconfig 命令更新此 IP:

That's the RetrofitClient class I use to connect the android app with the slim API, the BASE_URL is my PC IP, and BASE_URL_PC is my Laptop IP, enverytime I change my network connection I update this IPs using cmd and ipconfig command:

public class RetrofitClient {

    /* TODO: Preguntar a Juan porque no va ni en el portatil ni en el movil pero si en el fijo */

    private static final String BASE_URL = "http://147.96.127.212/CharmAppAPI/public/";

    private static final String BASE_URL_PC = "http://192.168.43.71/CharmAppAPI/public/";


    private static RetrofitClient instance;

    private Retrofit retrofit;

    private RetrofitClient(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL_PC)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static synchronized RetrofitClient getInstance(){
        if(instance == null){
            instance = new RetrofitClient();
        }
        return instance;
    }

    public APICalls getAPI(){
        return retrofit.create(APICalls.class);
    }

}

推荐答案

调试改造请使用日志拦截器

to debug retrofit please use logging interceptor

//apply dependencies
dependencies {
    implementation 'com.squareup.okhttp3:logging-interceptor:3.12.5'
}
//setup in retrofit
private RetrofitClient(){
    val logging=HttpLoggingInterceptor()
    logging.level=HttpLoggingInterceptor.Level.BODY
    val xclient=OkHttpClient.Builder()
    xclient.addInterceptor(logging)
    retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL_PC)
        .addConverterFactory(GsonConverterFactory.create())
        .client(xclient.build())
        .build();
}

如果您使用模拟器,本地主机 IP 为:http://10.0.2.2/

if you use emulator the localhost IP is: http://10.0.2.2/

这篇关于(Slim + Retrofit Android Studio App)请求不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 04:52
查看更多