我是java的android初学者。我无法使用 mvvm 架构登录,并在 android studio 中使用 JAVA 语言进行改造。我的代码如下所示:
我的代码如下所示:
API接口:
@FormUrlEncoded
@POST("/login")
Call<SignInResponse> getResponse(@Field("email") String email,
@Field("password") String password);
改造类:
private static final String BASE_URL = "meu dominio";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
// change your base URL
if (retrofit == null) {
OkHttpClient client = new OkHttpClient.Builder().readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS).build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
登录响应:
@SerializedName("id")
@Expose
public Integer id;
@SerializedName("name")
@Expose
public String name;
@SerializedName("email")
@Expose
public String email;
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id = id;
}
public String getName(){ return name;}
public void setName(String name){ this.name = name;}
public String getEmail(){ return email;}
public void setEmail(String email){this.email = email;}
登录存储库:
private Application application;
public SignInRepository(Application application) {
this.application = application;
}
public MutableLiveData<SignInResponse> getMutableData(String email, String password) {
final MutableLiveData<SignInResponse> mutableLiveData = new MutableLiveData<>();
ApiInterface apiInterface = RetroClass.getClient().create(ApiInterface.class);
Call<SignInResponse> call = apiInterface.getResponse(email, password);
call.enqueue(new Callback<SignInResponse>() {
@Override
public void onResponse(Call<SignInResponse> call, Response<SignInResponse> response) {
if (response.isSuccessful()) {
mutableLiveData.setValue(response.body());
} else {
SignInResponse signInResponse = new SignInResponse();
Integer id = signInResponse.getId();
mutableLiveData.setValue(signInResponse);
}
}
@Override
public void onFailure(Call<SignInResponse> call, Throwable t) {
SignInResponse signInResponse = new SignInResponse();
signInResponse.setId(signInResponse.id);
mutableLiveData.setValue(signInResponse);
}
});
return mutableLiveData;
}
meu 视图模型:
public class SignInViewModel extends AndroidViewModel {
private Context context;
private SignInRepository signInRepository;
public SignInViewModel(@NonNull Application application) {
super(application);
signInRepository = new SignInRepository(application);
}
public LiveData<SignInResponse> getResponse(String email, String password) {
return signInRepository.getMutableData(email, password);
}
}
主要活动 :
public class MainActivity extends AppCompatActivity {
EditText email_et, password_et;
SignInViewModel signInViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email_et = findViewById(R.id.etEmail);
password_et = findViewById(R.id.etPassword);
signInViewModel = new ViewModelProvider(this).get(SignInViewModel.class);
findViewById(R.id.SignIn_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!TextUtils.isEmpty(email_et.getText().toString())&& !TextUtils.isEmpty(password_et.getText().toString())){
SignInApiCall(email_et.getText().toString(),password_et.getText().toString());
}
else
Toast.makeText(MainActivity.this, "Please enter all the fields", Toast.LENGTH_SHORT).show();
}
});
}
private void SignInApiCall(String email, String password) {
signInViewModel.getResponse(email, password).observe(this, new Observer<SignInResponse>() {
@Override
public void onChanged(SignInResponse signInResponse) {
}
});
}
}
我确定我在某处犯了错误,但我不知道在哪里,当我进入调试模式时,会出现以下错误:E/NativeCrypto: Unknown error 1 during handshake
正在执行此代码以登录到 api,
构建.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"
implementation "com.squareup.picasso:picasso:2.5.2"
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
implementation 'com.squareup.retrofit2:converter-gson:2.8.1'
//noinspection GradleCompatible
implementation "com.android.support:recyclerview-v7:28.0.0"
implementation 'com.squareup.okhttp3:logging-interceptor:4.5.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.android.support:design:28.0.0'
implementation 'de.hdodenhof:circleimageview:3.1.0'
}
如果您发现代码中有任何错误,请告诉我。谢谢