java - 安装为 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。
任何帮助将不胜感激。
解决方案
我有回应:
我试图用 http 协议(不是 ssl)请求一个 web api。Android 确实阻止了它,因为它是一种不安全的数据传输方式。
推荐阅读
- office-ui-fabric - 如何在fluentui(office ui fabric)中创建一个“危险”按钮?
- sql - 订单中的所有捆绑产品(订单项目列表)
- ios - 添加项目时出现 Xamarin IOS 链接器错误
- azure - 如果文件中不包含“azuremediaplayer.min.js”,Azure 媒体播放器将不起作用
- java - src.type() == CV_8UC1 断言在校正图像时失败
- sql - 是否可以使用 ANALYZE 表指令创建过程?
- excel - 在执行 Office 脚本操作中指定 Excel 文件时出现问题
- python - 如何在 Python 中打印一条语句以显示对 Pandas Dataframe 的数学运算结果?
- c# - Blazor 在提交表单数据之前执行逻辑
- sql - presto 将单列值拆分为多行