嗨,在下面的代码中,我有登录应用程序。在其中包含电子邮件ID和密码。使用电子邮件ID和密码进行的注册很好。
现在,获取电子邮件ID和密码,然后按登录按钮,只是检查状态是否成功。如果状态为成功,则转到其他活动。
LoginActivity.java:
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
emailId = et_email.getText().toString();
password = et_password.getText().toString();
getlogindetails(emailId, password);
}
});
}
Method.java:
private void getlogindetails(String emailId, String password) {
if (!validate(emailId)) {
et_email.requestFocus();
et_email.setError("Please provide a valid email Id");
return;
}
if (password.isEmpty()) {
et_password.requestFocus();
et_password.setError("Please provide password");
return;
}
if (!CheckWifiAndMobileData.IsConnected(LoginActivity.this)) {
Toast.makeText(LoginActivity.this, "Please check your internet connection", Toast.LENGTH_SHORT).show();
return;
}
String url = "http://172.24.1.1:9000";
Retrofit retrofit = null;
Log.d("123", "retrofit");
if (retrofit == null) {
retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build();
Log.d("123", "build();");
}
API1 service = retrofit.create(API1.class);
Call<Login> call = service.authenticate(this.emailId, password);
Log.i(TAG, "Sending---" + url + service + "\n" + "light_id:" + this.emailId + "\n" + "intensity:" + password );
//final String EmailId = emailId;
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
String status=response.body().getStatus().toString();
if (status.contains("success")) {
// showResponse(response.body().toString());
Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
Intent mainIntent;
mainIntent = new Intent(LoginActivity.this, DeviceControlActivity.class);
mainIntent.putExtra("emailId", LoginActivity.this.emailId);
startActivity(mainIntent);
finish();
}
else{
Toast.makeText(LoginActivity.this, "Invalid EmailId and password", Toast.LENGTH_SHORT).show();
//
}
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API.");
}
});
}
API1.java
interface API1 {
@POST("app_login/")
Call<Login> authenticate(@Path("emailId") String emailId, @Path("password") String password);
}
答案 0 :(得分:1)
为什么在接口方法中使用
@Path
。它必须是@Body
或@Field
请勿使用@Path
@POST("app_login/")
Call<Login> authenticate(@Path("emailId") String emailId, @Path("password") String password);
尝试像这样使用,为您的请求创建Pojo
public class Request{
private String emailId;
private String password;
}
并像这样使用
@POST("app_login")
Call<Login> authenticate(@Body Request data);
或
像这样使用
@FormUrlEncoded
@POST("app_login")
Call<Login> authenticate(@Field("emailId") String emailId, @Field("password") String password);
注意:-当您的API @Path
相应地更改其他用户时,使用url
。例如,您正在根据用户ID提取数据。