(Slim + Retrofit Android Studio App)Requests not working

≡放荡痞女 提交于 2020-01-22 02:21:11

问题


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.

Anyone could know how to fix it?

Thanks a lot

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);
    }

}

回答1:


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();
}

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



来源:https://stackoverflow.com/questions/59408981/slim-retrofit-android-studio-apprequests-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!