I want to make a fitur to login, I want to make a feature to log in, initially I saw the response code shows 200, but when I saw the response body was null. I can still log in, but the logged in user data isn't saved. I used 2 db MySql and SQLite and im using retrofit and shared preference. And here my code
API Interface
public interface Api {
#FormUrlEncoded
#POST(Config.API_LOGIN_USER)
Call<UserOrtu> loginUser(
#Field("email") String email,
#Field("password") String password
);
PrefUtil.java
public class PrefUtil {
public static final String USER_SESSION = "user_session";
public static final String USER_STORAGE = "user_storage";
public static SharedPreferences getSharedPreferences(Context ctx){
return PreferenceManager.getDefaultSharedPreferences(ctx);
}
public static void putUser(Context ctx, String key, UserOrtu user){
Gson gson = new Gson();
String json = gson.toJson(user);
putString(ctx, key, json);
}
public static UserOrtu getUser(Context ctx, String key){
Gson gson = new Gson();
String json = getString(ctx, key);
UserOrtu user = gson.fromJson(json, UserOrtu.class);
return user;
}
public static void putString(Context ctx, String key, String value){
getSharedPreferences(ctx).edit().putString(key, value).apply();
}
public static String getString(Context ctx, String key){
return getSharedPreferences(ctx).getString(key, null);
}
public static void clear(Context ctx) {
getSharedPreferences(ctx).edit().clear().apply();
}
}
Models
UserOrtu.java
public class UserOrtu {
#SerializedName("id_user")
#Expose
private int idUser;
#SerializedName("nama")
#Expose
private String nama;
#SerializedName("email")
#Expose
private String email;
#SerializedName("password")
#Expose
private String password;
#SerializedName("error")
#Expose
private Boolean error;
#SerializedName("message")
#Expose
private String message;
public int getIdUser() {
return idUser;
}
public void setIdUser(int idUser) {
this.idUser = idUser;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Boolean getError() {
return error;
}
public void setError(Boolean error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
SignInActivity.java
public class SignInActivity extends AppCompatActivity {
#BindView(R.id.input_email_signin)
TextInputEditText etEmail;
#BindView(R.id.text_register)
TextView tvRegister;
#BindView(R.id.input_password_signin)
TextInputEditText etPassword;
EmailValidator emailValidator;
PasswordValidator passwordValidator;
Context context;
private String email;
private String password;
private Api mApi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isSessionLogin()){
startActivity(new Intent(this, MainActivity.class));
this.finish();
}
setContentView(R.layout.activity_sign_in);
ButterKnife.bind(this);
AndroidThreeTen.init(this);
mApi = RetrofitBuilder.builder(this).create(Api.class);
}
boolean isEmail(EditText text){
emailValidator = new EmailValidator();
String email = text.getText().toString();
return emailValidator.isValid(email);
}
boolean isPassword(EditText text){
passwordValidator = new PasswordValidator();
String pass = text.getText().toString();
return passwordValidator.isValid(pass);
}
#OnClick(R.id.text_register) void toRegister(){
Intent intent = new Intent(this, SignUpActivity.class);
startActivity (intent);
}
#OnClick(R.id.btn_signin) void onLogin(){
if(isEempty(etEmail)){
etEmail.setError("Email harus diisi");
}else if(isEempty(etPassword)){
etPassword.setError("Password harus diisi");
}else if(!isEmail(etEmail)){
etEmail.setError("Email tidak valid");
}else if(!isPassword(etPassword)){
String str = passwordValidator.getString();
Toast.makeText(getApplicationContext(),str, Toast.LENGTH_SHORT).show();
}else {
loginAct();
}
}
void loginAct(){
email = etEmail.getText().toString();
password = etPassword.getText().toString();
final MaterialDialog dialog = DialogBuilder.showLoadingDialog(SignInActivity.this, "Updating Data", "Please wait..", false);
mApi.loginUser(email, password).enqueue(new Callback<UserOrtu>() {
#Override
public void onResponse(Call<UserOrtu> call, Response<UserOrtu> response) {
UserOrtu user = response.body();
Log.i("USER_LOGIN", response.message());
if (user != null){
//Masih error disini
//if (!user.getError()){
PrefUtil.putUser(getApplicationContext(), PrefUtil.USER_SESSION, user);
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
//this.finish();
//}
Toast.makeText(getApplicationContext(), user.getMessage(), Toast.LENGTH_SHORT).show();
}
if (response.code() == 403){
etPassword.requestFocus();
etPassword.setError(getString(R.string.error_password));
}
if (response.code() == 404){
etEmail.requestFocus();
etEmail.setError(getString(R.string.error_login));
}
dialog.dismiss();
}
#Override
public void onFailure(Call<UserOrtu> call, Throwable t) {
//Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
Log.i("USER_LOGIN", t.getMessage());
DialogBuilder.showErrorDialog(SignInActivity.this, "Gagal Login");
}
});
}
// this method to check is user logged in ?
boolean isSessionLogin(){
return PrefUtil.getUser(getApplicationContext(), PrefUtil.USER_SESSION) != null;
}
}
And here's my result
enter image description here
Here's my JSON request
enter image description here
Your model is not representing same data as in JSON. With UserOrtu class it will works only if your response will have structure like below:
{
"id":1,
"nama": "Name",
"email":"email",
"message":"msg"
}
But as you can see 3 first fields are inside another object data. So your model should look more like:
class LoginResponse{
#SerializedName("data")
#Expose
private UserOrtu userData;
#SerializedName("message")
#Expose
private String message;
}
Related
I have an app that is to register people into a platform but I get a response of Unauthenticated each time I submit the form data. The form is submitted using an API which requires a bearer token for each post request with the aid of retrofit. I have been out of touch with Java.
Note: its just a plain form. No authentication has been implemented in the app.
My ApiClient.java class
public class ApiClient {
private static Retrofit getRetrofit(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("xxxxxxxxxxxxx")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
public static UserService getUserService(){
UserService userService = getRetrofit().create(UserService.class);
return userService;
}
}
My UserService.java class
public interface UserService {
#POST("algonapi/api/enroll_vehicle")
Call<UserResponse> saveUser(#Body UserRequest userRequest);
}
My saveUser Method
public void saveUser(UserRequest userRequest){
Call<UserResponse> userResponseCall = ApiClient.getUserService().saveUser(userRequest);
userResponseCall.enqueue(new Callback<UserResponse>() {
#Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful()){
Toast.makeText(MainActivity.this, "Registration Successfull! Click on Reset Form to Start a New Enumeration...", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this, "Registration Failed!", Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<UserResponse> call, Throwable t) {
Toast.makeText(MainActivity.this, "Registration Failed!" +t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
My UserRequest
package com.example.xxxxx;
public class UserRequest {
private String FullName;
private String StickerNumber;
private String Address;
private String Email;
private String Phone;
private String Nationality;
private String State;
private String LGA;
private String RC;
private String DriversLicenseNo;
private String LicenseIssued;
private String LicenseExpiry;
private String VehicleType;
private String VehicleLicense;
private String VehicleTyres;
private String LGAofOperation;
private String NOKFullName;
private String NOKAddress;
private String NOKPhone;
private String NOKEmail;
private String NOKNationality;
private String NOKState;
public String getFullName() {
return FullName;
}
public void setFullName(String fullName) {
FullName = fullName;
}
public String getStickerNumber() {
return StickerNumber;
}
public void setStickerNumber(String stickerNumber) {
StickerNumber = stickerNumber;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getNationality() {
return Nationality;
}
public void setNationality(String nationality) {
Nationality = nationality;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getLGA() {
return LGA;
}
public void setLGA(String LGA) {
this.LGA = LGA;
}
public String getRC() {
return RC;
}
public void setRC(String RC) {
this.RC = RC;
}
public String getDriversLicenseNo() {
return DriversLicenseNo;
}
public void setDriversLicenseNo(String driversLicenseNo) {
DriversLicenseNo = driversLicenseNo;
}
public String getLicenseIssued() {
return LicenseIssued;
}
public void setLicenseIssued(String licenseIssued) {
LicenseIssued = licenseIssued;
}
public String getLicenseExpiry() {
return LicenseExpiry;
}
public void setLicenseExpiry(String licenseExpiry) {
LicenseExpiry = licenseExpiry;
}
public String getVehicleType() {
return VehicleType;
}
public void setVehicleType(String vehicleType) {
VehicleType = vehicleType;
}
public String getVehicleLicense() {
return VehicleLicense;
}
public void setVehicleLicense(String vehicleLicense) {
VehicleLicense = vehicleLicense;
}
public String getVehicleTyres() {
return VehicleTyres;
}
public void setVehicleTyres(String vehicleTyres) {
VehicleTyres = vehicleTyres;
}
public String getLGAofOperation() {
return LGAofOperation;
}
public void setLGAofOperation(String LGAofOperation) {
this.LGAofOperation = LGAofOperation;
}
public String getNOKFullName() {
return NOKFullName;
}
public void setNOKFullName(String NOKFullName) {
this.NOKFullName = NOKFullName;
}
public String getNOKAddress() {
return NOKAddress;
}
public void setNOKAddress(String NOKAddress) {
this.NOKAddress = NOKAddress;
}
public String getNOKPhone() {
return NOKPhone;
}
public void setNOKPhone(String NOKPhone) {
this.NOKPhone = NOKPhone;
}
public String getNOKEmail() {
return NOKEmail;
}
public void setNOKEmail(String NOKEmail) {
this.NOKEmail = NOKEmail;
}
public String getNOKNationality() {
return NOKNationality;
}
public void setNOKNationality(String NOKNationality) {
this.NOKNationality = NOKNationality;
}
public String getNOKState() {
return NOKState;
}
public void setNOKState(String NOKState) {
this.NOKState = NOKState;
}
}
Create the OkHttpClient like this
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
#NotNull
#Override
public Response intercept(#NotNull Chain chain) throws IOException {
Request request=chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + token)
.build();
return chain.proceed(request);
}
}).build();
If you most of your https requests need authentication then the first answer is perfect but if some of your requests need then you can pass the header to each methods.
public interface UserService {
#POST("algonapi/api/enroll_vehicle")
Call<UserResponse> saveUser(
#Header("Authorization") String token,
#Body UserRequest userRequest
);
}
While calling the method simply pass your token along with userRequest.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I am trying to call login API using Retrofit2.
But in onResponse i alwasy get null as response.
Login API endpoint
#FormUrlEncoded
#POST("/api/login/{mobile}")
Call<ResObj> userLogin( #Field("phoneNumber") String mobile );
And the API implementation
private void doLogin(final String mobile){
Call<ResObj> call = userService.login(mobile);
call.enqueue(new Callback<ResObj>() {
#Override
public void onResponse(Call<ResObj> call, Response<ResObj> response) {
ResObj resObj = response.body(); // here i am getting null response.body()
if(resObj.getMessage().equals("true")){
Intent intent = new Intent(Login.this, ListActivity.class);
intent.putExtra("mobile", mobile);
startActivity(intent);
} else{
Toast.makeText(Login.this, "Phone Number is incorrect!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<ResObj> call, Throwable t) {
Toast.makeText(Login.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
ResObj class:
public class ResObj {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
I just want to know what causes the error and what are possible solutions.
UPDATE
POSTMAN
You are getting null response in your login API. It may be due to many reasons. You can check your API is working as expected or not using POSTMAN.
And inside your code, you can prevent this type of exception by checking OBJECT is null or not. like the following.
#Override
public void onResponse(Call<ResObj> call, Response<ResObj> response) {
ResObj resObj = response.body();
if(resObj != null){ // checking object is not null
if(resObj.getStatus()){
Intent intent = new Intent(Login.this, ListActivity.class);
intent.putExtra("mobile", mobile);
startActivity(intent);
} else{
Toast.makeText(Login.this, "Phone Number is incorrect!", Toast.LENGTH_SHORT).show();
}
}else{
// handle null response here.
}
}
Update:
According to your Response JSON, Your Model(ResObj) class should be like the following.
public class ResObj
{
private String date;
private String address;
private String accountName;
private String contactPerson;
private String timeOut;
private String problem;
private String srNo;
private String fieldEngineer;
private String joNo;
private String irNo;
private String designation;
private String email;
private String timeIn;
private String productType;
private boolean status;
private String contactNo;
public String getDate ()
{
return date;
}
public void setDate (String date)
{
this.date = date;
}
public String getAddress ()
{
return address;
}
public void setAddress (String address)
{
this.address = address;
}
public String getAccountName ()
{
return accountName;
}
public void setAccountName (String accountName)
{
this.accountName = accountName;
}
public String getContactPerson ()
{
return contactPerson;
}
public void setContactPerson (String contactPerson)
{
this.contactPerson = contactPerson;
}
public String getTimeOut ()
{
return timeOut;
}
public void setTimeOut (String timeOut)
{
this.timeOut = timeOut;
}
public String getProblem ()
{
return problem;
}
public void setProblem (String problem)
{
this.problem = problem;
}
public String getSrNo ()
{
return srNo;
}
public void setSrNo (String srNo)
{
this.srNo = srNo;
}
public String getFieldEngineer ()
{
return fieldEngineer;
}
public void setFieldEngineer (String fieldEngineer)
{
this.fieldEngineer = fieldEngineer;
}
public String getJoNo ()
{
return joNo;
}
public void setJoNo (String joNo)
{
this.joNo = joNo;
}
public String getIrNo ()
{
return irNo;
}
public void setIrNo (String irNo)
{
this.irNo = irNo;
}
public String getDesignation ()
{
return designation;
}
public void setDesignation (String designation)
{
this.designation = designation;
}
public String getEmail ()
{
return email;
}
public void setEmail (String email)
{
this.email = email;
}
public String getTimeIn ()
{
return timeIn;
}
public void setTimeIn (String timeIn)
{
this.timeIn = timeIn;
}
public String getProductType ()
{
return productType;
}
public void setProductType (String productType)
{
this.productType = productType;
}
public boolean getStatus ()
{
return status;
}
public void setStatus (boolean status)
{
this.status = status;
}
public String getContactNo ()
{
return contactNo;
}
public void setContactNo (String contactNo)
{
this.contactNo = contactNo;
}
}
You are passing parameter as raw data(according to your screen-shot). So your API endpoint would be like below.
#Headers("Content-Type: application/json")
#POST("/api/login")
Call<ResObj> userLogin(#Body JsonObject jsonObject);
And call your API like this
private void doLogin(final String mobile){
try {
JsonObject paramObject = new JsonObject();
paramObject.addProperty("mobile", mobile);
} catch (JSONException e) {
e.printStackTrace();
}
Call<ResObj> call = userService.login(paramObject);
call.enqueue(new Callback<ResObj>() {
//your rest of code
});
}
UPDATE-2:
To send object from one Activity to another using intent you have to make your model class Percelable. like this
// implements Parcelable
public class ResObj implements Parcelable {
// ...........your previous code here
// just simply add the following methods
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(date);
dest.writeString(address);
dest.writeString(accountName);
dest.writeString(contactPerson);
dest.writeString(timeOut);
dest.writeString(problem);
dest.writeString(srNo);
dest.writeString(fieldEngineer);
dest.writeString(joNo);
dest.writeString(irNo);
dest.writeString(designation);
dest.writeString(email);
dest.writeString(timeIn);
dest.writeString(productType);
dest.writeByte((byte) (status ? 1 : 0));
dest.writeString(contactNo);
}
public static final Parcelable.Creator<ResObj> CREATOR
= new Parcelable.Creator<ResObj>() {
public ResObj createFromParcel(Parcel in) {
return new ResObj(in);
}
public ResObj[] newArray(int size) {
return new ResObj[size];
}
};
protected ResObj(Parcel in) {
date = in.readString();
address = in.readString();
accountName = in.readString();
contactPerson = in.readString();
timeOut = in.readString();
problem = in.readString();
srNo = in.readString();
fieldEngineer = in.readString();
joNo = in.readString();
irNo = in.readString();
designation = in.readString();
email = in.readString();
timeIn = in.readString();
productType = in.readString();
status = in.readByte() != 0;
contactNo = in.readString();
}
}
Now pass your object via intent like the following.
if(resObj != null){
if(resObj.getStatus()){
Intent intent = new Intent(Login.this, ListActivity.class);
intent.putExtra("your_key", resObj); // pass resObj and use same key to get data
startActivity(intent);
} else{
Toast.makeText(Login.this, "Phone Number is incorrect!", Toast.LENGTH_SHORT).show();
}
}
Get data from your ListActivity like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final ResObj yourObject = getIntent().getParcelableExtra("your_key"); // make sure you use same key like data.
// Now you can use your data like that
yourEditText.setText(yourObject.getEmail());
}
Am using retrofit and RxJava to Connect to API. Data is being successfully posted to Server but i would like to get the Token which the API generates after posting data.
This is for Authentication of user using a JSON Web Token Django Backend
The models are working fine but Here are my Models:
public class User {
#SerializedName("user")
#Expose
private User_ user;
public User_ getUser() {
return user;
}
public void setUser(User_ user) {
this.user = user;
}
#Override
public String toString() {
return new ToStringBuilder(this).append("user", user).toString();
}
}
public class User_ {
#SerializedName("email")
#Expose
private String email;
#SerializedName("password")
#Expose
private String password;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
#Override
public String toString() {
return new ToStringBuilder(this).append("email", email).append("password", password).toString();
}
}
#SerializedName("user")
#Expose
private LoginResponse_ user;
public LoginResponse_ getUser() {
return user;
}
public void setUser(LoginResponse_ user) {
this.user = user;
}
#Override
public String toString() {
return new ToStringBuilder(this).append("user", user).toString();
}
}
public class LoginResponse_ {
#SerializedName("email")
#Expose
private String email;
#SerializedName("token")
#Expose
private String token;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
#Override
public String toString() {
return new ToStringBuilder(this).append("email", email).append("token", token).toString();
}
}
Retrofit Interface
#POST("auth/login")
#Headers("X-Requested-With:XMLHttpRequest")
Observable<User> login(
#Header("Content-Type") String content_type,
#Body User user
);
}
Retrofit Adapter
public class ServiceGenerator {
private static Retrofit retrofit;
private static Gson gson;
public static synchronized Retrofit getUser() {
if (retrofit == null) {
if (gson == null) {
gson = new GsonBuilder().setLenient().create();
}
retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
Retrofit Instance
public class NetworkUtils {
private static UserService userService;
public static UserService ApiInstance(){
if (userService == null){
userService = ServiceGenerator.getUser().create(UserService.class);
}
return userService;
}
}
This is how am trying to login the user
private void loginProcess(String email, String password) {
User user = new User();
User_ user_ = new User_();
user_.setEmail(email);
user_.setPassword(password);
user.setUser(user_);
mUserService.login("application/json", user)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<User>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(User user) {
showResponse(user.toString());
Log.i(TAG, "Response: "+ user);
mProgressBar.setVisibility(View.GONE);
handleResponse();
}
#Override
public void onError(Throwable e) {
e.printStackTrace();
}
#Override
public void onComplete() {
}
});
}
private void handleResponse(LoginResponse response) {
SharedPreferences.Editor editor = mSharedPreference.edit();
editor.putString(Constants.TOKEN,response.getUser().getToken());
editor.putString(Constants.EMAIL,response.getUser().getEmail());
editor.apply();
mEditTextEmail.setText(null);
mEditTextPassword.setText(null);
Intent intent = new Intent(getActivity(),ProfileActivity.class);
startActivity(intent);
}
It would be really helpful if i get to know how to get response after posting the data to the API.
Using postman this is what i post
{"user":{"email":"user#email.com", "password":"userpassword"}}
And this is the response I get
{
"user": {
"email": "user#email.com",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NSwiZXhwIjoxNTY5MjI5NTgxfQ.S-1yZWNhx_TV8uay8PubGoq9XMpyTIn_ipG8A6DWTfE"
}
}
I was using FirebaseMessagingService to send a notification to other users using my app, the problem is I cannot add extra data to the message. I have a notification, sender, and data model classes. Problem is when I try adding the data model when sending the token however I get invalid JSON.
Notifcation class:
public String title;
public String body;
public Notification() {
}
public Notification(String title, String body) {
this.title = title;
this.body = body;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
Sender class:
public class Sender {
public Notification data;
public data extra;
public String to;
public Sender() {
}
public Sender(Notification data, data extra, String to) {
this.data = data;
this.to = to;
this.extra = extra;
}
public Notification getData() {
return data;
}
public void setData(Notification data) {
this.data = data;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}
data class:
public class data {
public String username;
public data() {
}
public data(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Home:
data extra = new data("Test");
Notification notification = new Notification("title test", "body test");
Sender content = new Sender(notification, extra, token.getToken());
mService.sendMessage(content)
.enqueue(new Callback<FCMResponse>() {
#Override
public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
Log.i(TAG, "onResponse: " + response.toString());
if (response.body().success == 1){
Toast.makeText(c, "Request sent!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(c, "Request failed!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<FCMResponse> call, Throwable t) {
Log.e(TAG, "onFailure: "+ t.getMessage());
}
});
MyFirebaseMessaingService:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().isEmpty()){
} else {
String title = remoteMessage.getData().get("title").toString();
String body = remoteMessage.getData().get("body").toString();
String username = remoteMessage.getData().get("username").toString();
Log.i(TAG, "showNotifcation: " + title + body + username);
}
}
Problem occurs when i send extra data from the model class i.e the username. Without send extra data i.e just the notification it will work fine.
I want to register a user with their token, I want to register a user using retrofit in android but I keep getting this error:
ERROR::: Attempt to invoke virtual method 'void com.signup.User.setUsername(java.lang.String)' on a null object reference
Here is my code:
public class Session {
Context context;
private SharedPreferences prefs;
public Session(Context cntx) {
// TODO Auto-generated constructor stub
this.context = cntx;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
public void setJwtToken(String token) {
prefs.edit().putString("JwtToken", token).commit();
}
public String getJwtToken() {
String token = prefs.getString("JwtToken", "");
if (token == null || token.isEmpty()) {
token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
}
return token;
}
}
public interface ApiInterface {
#POST("/api/users/signup")
Call<ResponseBody> signMeUp(#Header("Authorization") String token ,#Body User user);
}
public class MainActivity extends AppCompatActivity {
private EditText et_name, et_address, et_phone, et_username, et_email, et_password, et_confipassword;
private Button register;
private User user;
private SharedPreferences prefs;
private ApiInterface apiInterface;
private Session session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = findViewById(R.id.edit_text_name);
et_address = findViewById(R.id.edit_text_address);
et_phone = findViewById(R.id.edit_text_phonenumber);
et_username = findViewById(R.id.edit_text_username);
et_email = findViewById(R.id.edit_text_email);
et_password = findViewById(R.id.edit_text_password);
et_confipassword = findViewById(R.id.edit_text_confirm_password);
register = findViewById(R.id.signupButton);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Login();
}
});
}
private void Login() {
user.setUsername(et_name.getText().toString());
user.setAddress(et_address.getText().toString());
user.setPhone(et_phone.getText().toString());
user.setName(et_name.getText().toString());
user.setEmail(et_email.getText().toString());
user.setPassword(et_password.getText().toString());
user.setPasswordConfirmation(et_confipassword.getText().toString());
signupUser(user);
}
private void signupUser(final User user) {
// Set up progressbar before call
apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<ResponseBody> call1 = apiInterface.signMeUp(session.getJwtToken(),user);
final Gson gson = new Gson();
final String json = gson.toJson(user);
call1.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.code() == 201) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
//Starting main activity after user sees dialog
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (response.code() == 500) {
try {
JSONObject jsonObject = new JSONObject(response.errorBody().string());
Log.e("SignupFragment", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else
Log.e("SignupFragment", response.raw().toString());
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
}
public class User {
#SerializedName("name")
#Expose
private String name;
#SerializedName("email")
#Expose
private String email;
#SerializedName("password")
#Expose
private String password;
#SerializedName("password_confirmation")
#Expose
private String passwordConfirmation;
#SerializedName("image")
#Expose
private String image;
#SerializedName("phone")
#Expose
private String phone;
#SerializedName("address")
#Expose
private String address;
#SerializedName("username")
#Expose
private String username;
#SerializedName("pan_no")
#Expose
private String panNo;
#SerializedName("birthday")
#Expose
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPasswordConfirmation() {
return passwordConfirmation;
}
public void setPasswordConfirmation(String passwordConfirmation) {
this.passwordConfirmation = passwordConfirmation;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPanNo() {
return panNo;
}
public void setPanNo(String panNo) {
this.panNo = panNo;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
public class ApiClient {
public static final String BASE_URL = "https://myapp.herokuapp.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
ApiInterface apiInterface=retrofit.create(ApiInterface.class);
}
I want to signup a user via my app but I keep getting the error. And, if I remove the auth from header from my Interface I get a message asking a token, and when I provide a token it give me a null object reference error !!
You should instantiate variable before access it: User user = new User();
Update: do it in void Login()
create an instance of User inside onCreate method or up there where it's declared
Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
user = new User();
session = new Session(this);
}