简体   繁体   English

安装为 apk 时,retrofit2 连接抛出错误

[英]retrofit2 connection is throwing errors when installed as apk

I'm connecting to restful api with retrofit2.我正在使用retrofit2连接到restful api。

When I try the code in the emulator console, it runs fine and connect to my web api.当我在模拟器控制台中尝试代码时,它运行良好并连接到我的 web api。

But When I convert that to apk release mode and install it on my cell phone, then the code doesn't work.但是当我将其转换为 apk 发布模式并将其安装在我的手机上时,该代码不起作用。

Do you know if this is an normal behavior and when I upload to google playstore it will work fine?你知道这是否是正常行为,当我上传到谷歌游戏商店时它会正常工作吗?

    public void login(String email, String password)
    {
        //search at web api
        final User[] user = {new User()};
        APIInterface apiInterface;
        apiInterface = APIClient.getClient("http://url.com").create(APIInterface.class);
        Call<User> call1 = apiInterface.UserLogin(email, password);
        call1.enqueue(new Callback<User>()
        {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful()) {
                    if (response.body().Id > 0)
                    {
                        user[0].Id = response.body().Id;
                        user[0].email = email;
                        user[0].tokenuser = response.body().tokenuser;
                        user[0].tokensession = response.body().tokensession;

                        //correct credentials
                        SaveOnPreferences(email, password);

                        Intent intent = new Intent(LoginForm.this, Dashboard.class);
                        startActivity(intent);
                        finish();
                    }
                    else
                    {
                        //wrong password
                        Toast.makeText(getApplicationContext(), R.string.IncorrectCredentials, Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t)
            {
                Toast.makeText(getApplicationContext(), "public void onFailure: " + user[0].Id, Toast.LENGTH_SHORT).show();
                call.cancel();
            }
        });
    }
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.converter.gson.GsonConverterFactory;

class APIClient {

    private static Retrofit retrofit = null;

    static Retrofit getClient(String url) {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        //interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


        retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

        return retrofit;
    }

}
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

public interface APIInterface {

    @POST("/miracle/user/login")
    @FormUrlEncoded
    Call<User> UserLogin(@Field("email") String Email, @Field("password") String Password);

}
import com.google.gson.annotations.SerializedName;

public class User
{
    @SerializedName("Id")
    public int Id;

    @SerializedName("Fullname")
    public String fullname;

    @SerializedName("Email")
    public String email;

    @SerializedName("Password")
    public String password;

    @SerializedName("ValidEmail")
    public boolean validemail;

    @SerializedName("EnableNotification")
    public boolean enablenotification;

    @SerializedName("TokenUser")
    public String tokenuser;

    @SerializedName("TokenSession")
    public String tokensession;

    public  User(){

    }
...
}
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.xxx.zzz"
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

The cellphone where I'm installing the apk is Huawei P30.我安装apk的手机是华为P30。

Any help will be very appreciated.任何帮助将不胜感激。

I have the response:我有回应:

I was trying to request a web api with http protocol (not ssl).我试图请求一个带有 http 协议(不是 ssl)的 web api。 Android does prevent it because it is an insecure way to transmit data. Android 确实阻止了它,因为它是一种不安全的数据传输方式。

The solution was found here: Android 8: Cleartext HTTP traffic not permitted解决方案在这里找到: Android 8: Cleartext HTTP traffic not allowed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM