I created Retrofit interface
public interface UserService {
@GET(Constants.Api.URL_LOGIN)
Call<String> loginUser(@Query("email") String email, @Query("password") String pass, @Query("secret") String secret, @Query("device_id") String deviceid, @Query("pub_key") String pubkey, @Query("device_name") String devicename);
When in Activity I call
final Call<String> responce = service.loginUser(loginedt.getText().toString(), md5(passwordedt.getText().toString()), secret, device_id, pub_key, device_name);
responce.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response) {
if (response.code() == Constants.Status.ERROR_404) {
Toast.makeText(LoginActivity.this, getResources().getString(R.string.wrong_log_pass), Toast.LENGTH_LONG).show();
} else if (response.code() != Constants.Status.ERROR_404 && response.code() != Constants.Status.SUCCES) {
Toast.makeText(LoginActivity.this, getResources().getString(R.string.wrong_request), Toast.LENGTH_LONG).show();
} else {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});
I am getting error
onFailure java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path $
If you don't create your own models, use Call<JsonObject>
.
This is the default parser for application/json content type requests.
What is the response of your API? Retrofit parses the server response into the type passed to call object, i.e. Call<ResponseType>
. As from error you are receiving, server returns object while you're expecting a string.
change your service to be
Call<ResponseTypeObject> loginUser(@Query("email") String email, @Query("password") String pass, @Query("secret") String secret, @Query("device_id") String deviceid, @Query("pub_key") String pubkey, @Query("device_name") String devicename);
where ResponseTypeObject
is the response entity as returned from server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With