0

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

4
  • problem is in your response. You are telling retrofit that you'll get a successful response of User, but actually is the Token Commented Sep 11, 2019 at 11:49
  • 1
    It's beacause you're asking response as User model (Response<User>), so it's checking for email and password, but it's getting string, So change Response type to Response<String>. Commented Sep 11, 2019 at 11:51
  • @AhsanAbdullah If it helps, Please mark it as an answer or upvote . Commented Sep 11, 2019 at 12:01
  • Possible duplicate of JsonSyntaxException Gson: Expected BEGIN_OBJECT but was STRING Commented Sep 11, 2019 at 12:02

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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());
        }
    });
}

Comments

0

Replcae Response<User> with Response<ResponseBody>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.