我正在使用Retrofit与Android上的REST API通信,但是 得到如下所示的NullPointerException。我尝试使用邮递员, 该API正常工作,并且我得到登录名正常工作的响应。
错误
进程:com.example.krish.webdemo,PID:3064 java.lang.NullPointerException 在com.example.krish.webdemo.activities.LoginActivity $ 2.onResponse
Call<LoginResponse> call = RetrofitClient
.getInstance()
.getApi()
.userLogin(email, password);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
LoginResponse loginResponse = response.body();
if (!loginResponse.isError()) {
Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
}
});
}
我的登录响应有一个NullPointerException
,“。isError()”为
显示空错误
API接口
@FormUrlEncoded
@POST("userlogin")
Call<LoginResponse> userLogin(
@Field("email") String email,
@Field("password") String password
);
改造实例
private static final String BASE_URL ="http://192.168.1.38/KrishApi/public/";
private static RetrofitClient mInstance;
private Retrofit retrofit;
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getInstance() {
if (mInstance == null) {
mInstance = new RetrofitClient();
}
return mInstance;
}
public Api getApi(){
return retrofit.create(Api.class);
}
JSON响应
{
"error": false,
"message": "Login Successful",
"user": {
"id": 3,
"email": "pandi@gmail.com",
"name": "pandi",
"age": "22",
"college": "sec"
}
}
POJO类
public class LoginResponse {
private boolean error;
private String message;
private User user;
public LoginResponse(boolean error, String message, User user) {
this.error = error;
this.message = message;
this.user = user;
}
public boolean isError() {
return error;
}
public String getMessage() {
return message;
}
public User getUser() {
return user;
}
}
答案 0 :(得分:1)
public class CorsHandler : DelegatingHandler
{
const string Origin = "Origin";
const string AccessControlRequestMethod = "Access-Control-Request-Method";
const string AccessControlRequestHeaders = "Access-Control-Request-Headers";
const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
const string AccessControlAllowMethods = "Access-Control-Allow-Methods";
const string AccessControlAllowHeaders = "Access-Control-Allow-Headers";
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
bool isCorsRequest = request.Headers.Contains(Origin);
bool isPreflightRequest = request.Method == HttpMethod.Options;
if (isCorsRequest)
{
if (isPreflightRequest)
{
return Task.Factory.StartNew<HttpResponseMessage>(() =>
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Headers.Add(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());
string accessControlRequestMethod = request.Headers.GetValues(AccessControlRequestMethod).FirstOrDefault();
if (accessControlRequestMethod != null)
{
response.Headers.Add(AccessControlAllowMethods, accessControlRequestMethod);
}
string requestedHeaders = string.Join(", ", request.Headers.GetValues(AccessControlRequestHeaders));
if (!string.IsNullOrEmpty(requestedHeaders))
{
response.Headers.Add(AccessControlAllowHeaders, requestedHeaders);
}
return response;
}, cancellationToken);
}
else
{
return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(t =>
{
HttpResponseMessage resp = t.Result;
resp.Headers.Add(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());
return resp;
});
}
}
else
{
return base.SendAsync(request, cancellationToken);
}
}
}
返回null。因此,您必须始终在response.body()
方法内检查响应是否成功。
onResponse
答案 1 :(得分:0)
"error": false,
当应用程序尝试使用NullPointerException 具有空值的对象引用。
您应该检查您的 响应 isSuccess()
。
/ ** {@code true},如果{@link #code()}在[200..300)范围内。 * /
公共布尔isSuccess(){ 返回rawResponse.isSuccessful(); }
if (response.isSuccess()) //isSuccessful()
{
LoginResponse loginResponse = response.body();
////
}