简体   繁体   English

使用Retrofit查询Uber API

[英]Using Retrofit to query Uber API

I am trying to get products json data from https://api.uber.com/v1/products ( https://developer.uber.com/docs/v1-products ) but I am getting the following error. 我试图从获得产品的JSON数据https://api.uber.com/v1/productshttps://developer.uber.com/docs/v1-products ),但我收到以下错误。

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)

Here is the relevant code 这是相关的代码

RestAPI RESTAPI

public interface RestAPI {
@GET("/v1/products")
Call<ProductsResponse> load(@Header("Authorization") String authToken,
                            @Query("latitude") float latitude,
                            @Query("longitude") float longitude);
}

ProductsResponse ProductsResponse

public class ProductsResponse {
public List<ProductsPOJO> products;
}

Code from the Activity 来自活动的代码

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

}

Model Class 模型类

public class ProductsPOJO {
public String display_name;
public String description;
public ProductsPOJO()
{

}

public String getDisplay_name(){
    return display_name;
}
public String getDescription(){
    return description;
}}

I think it has something to do with authorization and there is some problem with my get request. 我认为这与授权有关,我的获取请求存在一些问题。 Kindly point the issue, thanks in advance. 请提前感谢,请指出这个问题。

Fixed the problem. 解决了这个问题。 Added Bearer to the access token before passing it. 在传递之前向访问令牌添加了承载。

String t= "Bearer "+accessTokenManager.getAccessToken().getToken();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM