gpt4 book ai didi

java - Retrofil 尝试对空对象调用接口(interface)方法

转载 作者:行者123 更新时间:2023-12-02 02:14:49 25 4
gpt4 key购买 nike

我尝试使用 Android 版 Retrofit 通过 http 获取 JSON 数据,但出现以下错误:

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

这是我的类(class)。

客户端

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

最佳答案

需要创建您的实现:

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

关于java - Retrofil 尝试对空对象调用接口(interface)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com