简体   繁体   中英

Response error :: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

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

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.

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

Response<ResponseBody>替换Response<User> Response<ResponseBody>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM