首页 > 解决方案 > 安装为 apk 时,retrofit2 连接会引发错误

问题描述

我正在使用retrofit2 连接到restful api。

当我在模拟器控制台中尝试代码时,它运行良好并连接到我的 web api。

但是当我将其转换为 apk 发布模式并将其安装在我的手机上时,代码不起作用。

你知道这是否是正常行为,当我上传到 google playstore 时它会正常工作吗?

    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"
    }

我安装apk的手机是华为P30。

任何帮助将不胜感激。

标签: javaandroidretrofit2

解决方案


我有回应:

我试图用 http 协议(不是 ssl)请求一个 web api。Android 确实阻止了它,因为它是一种不安全的数据传输方式。

解决方案在这里找到:Android 8: Cleartext HTTP traffic not allowed


推荐阅读