1

I am trying to get JSON data via http using Retrofit for Android I am getting the following error:

Attempt to invoke interface method 'retrofit2.Call com.webdealer.otexconnect.goldback.network.LoginServices.sendSMS(java.lang.String)' on a null object reference

Here are my classes.

Client

package com.webdealer.otexconnect.goldback.network;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by fazal on 3/13/18.
 */

public class RetrofitClient {

    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl) {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

    public void getServies(){

       // return RetrofitClient.getClient(BASE_URL).create(LoginServices.class);
    }
}

Interface:

package com.webdealer.otexconnect.goldback.network;

import com.webdealer.otexconnect.goldback.utils.Login;
import com.webdealer.otexconnect.goldback.utils.Sms;

import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.Path;

/**
 * Created by fazal on 3/13/18.
 */

public interface LoginServices {

    @FormUrlEncoded
    @POST("login")
    Call<Login> login(@Field("email") String email, @Field("password") String password);
    @FormUrlEncoded
    @POST("register-customer")
    Call<JSONObject> signUp(@Field("name") String name,@Field("email") String email,@Field("phone_number") String phoneNumber, @Field("password") String password);@FormUrlEncoded
    @POST("register-driver")
    Call<JSONObject> signUpDriver(@Field("name") String name,@Field("email") String email,@Field("phone_number") String phoneNumber, @Field("password") String password);
    @FormUrlEncoded
    @POST("send-sms-code")
    Call<Sms> sendSMS (@Field("phone_number") String phone_number);



}

Model:

package com.webdealer.otexconnect.goldback.utils;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Sms {

    @SerializedName("status")
    @Expose
    private Boolean status;
    @SerializedName("fourDigitCode")
    @Expose
    private String fourDigitCode;

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public String getFourDigitCode() {
        return fourDigitCode;
    }

    public void setFourDigitCode(String fourDigitCode) {
        this.fourDigitCode = fourDigitCode;
    }

}


try {

                    mLoginServices.sendSMS("+923418007173").enqueue(new Callback<Sms>(){

                        @Override
                        public void onResponse(Call<Sms> call, Response<Sms> response) {
                            Toast.makeText(getApplicationContext(), mPhone, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(Call<Sms> call, Throwable t) {
                            Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_LONG).show();
                        }
                    });

                }catch (Exception ex){
                    Log.e("Error sms",ex.getMessage());
                }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Fazal Mabood
  • 51
  • 2
  • 8

1 Answers1

3

Your implementation needs to be created:

public class RetrofitClient {

    private static LoginServices apiService;

    public RetrofitClient(String baseUrl) {

           Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

         apiService = retrofit.create(LoginServices.class);
    }

    public LoginServices getServies(){
        return apiService;
    }
}

Now when you call the service you just need to do:

RetrofitClient client = new RetrofitClient(BASE_URL);
client.getServies().sendSMS("+923418007173").enqueue(new Callback<Sms>(){

                        @Override
                        public void onResponse(Call<Sms> call, Response<Sms> response) {
                            Toast.makeText(getApplicationContext(), mPhone, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(Call<Sms> call, Throwable t) {
                            Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_LONG).show();
                        }
                    });
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46