Retrofil尝试在null对象上调用接口方法调用

时间:2018-03-23 23:30:55

标签: java android nullpointerexception

我正在尝试使用Retrofit for Android通过http获取JSON数据我收到以下错误:

尝试调用接口方法' retrofit2.Call com.webdealer.otexconnect.goldback.network.LoginServices.sendSMS(java.lang.String)'在空对象引用上

这是我的课程。

客户端

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

接口:

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



}

型号:

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

1 个答案:

答案 0 :(得分:0)

您的实施需要创建:

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

现在,当您拨打该服务时,您只需要这样做:

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