Reputation: 47
I am making user authentication in android. i am passing username and password but this error appears. API is working fine any help will be appreciated Can anyone tell i am doing wrong
This is the the error that i am getting in android
[![enter image description here][1]][1]
LoginActivity
public class Login extends AppCompatActivity {
private String email,password;
private EditText emailET;
private EditText passwordET;
private Button loginButton,createAccount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
emailET = findViewById(R.id.editTextUname);
passwordET = findViewById(R.id.editTextPassword);
loginButton = findViewById(R.id.loginButton);
createAccount = findViewById(R.id.createaccountButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userAuth();
}
});
createAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Login.this,SignUp.class);
startActivity(i);
}
});
}
private void userAuth() {
VidlyEndPoint vidlyEndPoint = RetrofitInstance.getService().create(VidlyEndPoint.class);
email = emailET.getText().toString();
password = passwordET.getText().toString();
JsonObject requestBody = new JsonObject();
requestBody.addProperty("email",email );
requestBody.addProperty("password",password );
Log.e("Response", String.valueOf(requestBody));
// User user = new User(email, password);
Call<User> call = vidlyEndPoint.authUser(requestBody) ;
final Intent dash = new Intent(getApplicationContext(),dashboard.class);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (!response.isSuccessful()) {
Toast.makeText(getApplicationContext(),"Invalid Email or Password", Toast.LENGTH_SHORT).show();
Log.e("Response NS :", String.valueOf(response.code()));
}
else if(response.code() == 200){
Toast.makeText(getApplicationContext(), response.body().toString(), Toast.LENGTH_SHORT).show();
startActivity(dash);
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Log.e("Response error :", t.getMessage());
}
});
}
}
Expected output: JSON TOKEN
Upvotes: 0
Views: 478
Reputation: 3930
You get This exception because you want an object
in response but the actual response
you got is a string
.
You have to change your server response
as JSON or your response type
in API
service as String.
Upvotes: 1
Reputation: 7209
try this, with String response not User
Call<String> call = vidlyEndPoint.authUser(requestBody) ;
final Intent dash = new Intent(getApplicationContext(),dashboard.class);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (!response.isSuccessful()) {
Toast.makeText(getApplicationContext(),"Invalid Email or Password", Toast.LENGTH_SHORT).show();
Log.e("Response NS :", String.valueOf(response.code()));
}
else if(response.code() == 200){
Toast.makeText(getApplicationContext(), response.body().toString(), Toast.LENGTH_SHORT).show();
startActivity(dash);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("Response error :", t.getMessage());
}
});
}
Upvotes: 0