我正在尝试从https://api.uber.com/v1/products(https://developer.uber.com/docs/v1-products)获取产品json数据,但我收到以下错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: shaurya.uberintegration, PID: 21380
java.lang.NullPointerException: Attempt to read from field 'java.util.Listshaurya.uberintegration.ProductsResponse.products' on a null object reference
at shaurya.uberintegration.Success.onResponse(Success.java:40)
at retrofit.ExecutorCallAdapterFactory$ExecutorCallback$1.run(ExecutorCallAdapterFactory.java:86)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
以下是相关代码
RESTAPI
public interface RestAPI {
@GET("/v1/products")
Call<ProductsResponse> load(@Header("Authorization") String authToken,
@Query("latitude") float latitude,
@Query("longitude") float longitude);
}
ProductsResponse
public class ProductsResponse {
public List<ProductsPOJO> products;
}
活动代码
AccessTokenManager accessTokenManager=new AccessTokenManager(this);
String t= accessTokenManager.getAccessToken().getToken();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.uber.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
RestAPI restAPI = retrofit.create(RestAPI.class);
Call<ProductsResponse> call = restAPI.load(t, 28.632328f, 77.216858f);
call.enqueue(this);
}
@Override
public void onResponse(Response<ProductsResponse> response, Retrofit retrofit) {
Toast.makeText(getApplicationContext(),response.body().products.get(0).getDescription(),Toast.LENGTH_SHORT)
.show();
}
@Override
public void onFailure(Throwable t) {
}
模型类
public class ProductsPOJO {
public String display_name;
public String description;
public ProductsPOJO()
{
}
public String getDisplay_name(){
return display_name;
}
public String getDescription(){
return description;
}}
我认为这与授权有关,我的获取请求存在一些问题。请提前感谢,请提出问题。
答案 0 :(得分:0)
修正了问题。在传递之前向访问令牌添加了承载。
String t= "Bearer "+accessTokenManager.getAccessToken().getToken();