简体   繁体   English

解析Retrofit2响应时出错

[英]Error when parsing retrofit2 response

I'm lost as to where and what I need to change to get this to work. 我迷失了在什么地方以及需要进行哪些更改才能使其正常工作。

I'm using this API call to yahoo finance. 我正在将此 API调用用于Yahoo Finance。

This is my retrofit instance: 这是我的改造实例:

public class ApiClient {
    private static Retrofit retrofit = null;
    private static final String BASE_URL = "https://query.yahooapis.com";

    public static Retrofit getClient(){
        if(retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

Where i make my call: 我打电话的地方:

String query = "select * from yahoo.finance.quotes where symbol in (\"YHOO\",\"AAPL\",\"GOOG\",\"2MSFT\")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";

YahooApi yahooApi = ApiClient.getClient().create(YahooApi.class);
Call<GetStockResponse> call = yahooApi.getStock(query);

call.enqueue(new Callback<GetStockResponse>() {
    @Override
    public void onResponse(Call<GetStockResponse> call, Response<GetStockResponse> response) {
        List<Stock> list = response.body().getQuery().getResults().getList();
        if(list!=null)
            Toast.makeText(getContext(), list.size(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onFailure(Call<GetStockResponse> call, Throwable t) {

    }
});

Interface (YahooApi): 界面(YahooApi):

@GET("v1/public/yql")
Call<GetStockResponse> getStock(@Query("q") String query);

Based on the JSON object I get back, I made the following wrapper classes: 基于我得到的JSON对象,我制作了以下包装器类:

public class GetStockResponse {
    @SerializedName("query")
    @Expose
    private Query mQuery;

    public Query getQuery(){
        return mQuery;
    }
}


public class Query {
    @SerializedName("count")
    @Expose
    private String mCount;
    @SerializedName("results")
    @Expose
    private Result mResults;

    public String getCount(){
        return mCount;
    }

    public Result getResults(){
        return mResults;
    }
}

public class Result {
    @SerializedName("quote")
    @Expose
    private List<Stock> mList;

    public List<Stock> getList(){
        return mList;
    }
}

public class Stock {
    @SerializedName("symbol")
    @Expose
    private String mSymbol;
    @SerializedName("Bid")
    @Expose
    private String mBid;
    @SerializedName("Change")
    @Expose
    private String mChange;
    @SerializedName("PercentChange")
    @Expose
    private String mPercentChange;
    @SerializedName("Name")
    @Expose
    private String mName;

    public String getSymbol(){
        return mSymbol;
    }

    public String getBid(){
        return mBid;
    }
    public String getChange(){
        return mChange;
    }

    public String getPercentChange(){
        return mPercentChange;
    }

    public String getName(){
        return mName;
    }
}

Any information on what i need to change will be greately appreciated! 我需要更改的任何信息将不胜感激!

Edit* added error message: 编辑*添加错误消息:

   java.lang.NullPointerException: Attempt to invoke virtual method 'strahinja.udacity.com.stockhawk.model.Query strahinja.udacity.com.stockhawk.model.GetStockResponse.getQuery()' on a null object reference
             at strahinja.udacity.com.stockhawk.fragment.MainFragment$1.onResponse(MainFragment.java:64)
             at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
             at android.os.Handler.handleCallback(Handler.java:739)
             at android.os.Handler.dispatchMessage(Handler.java:95)
             at android.os.Looper.loop(Looper.java:158)
             at android.app.ActivityThread.main(ActivityThread.java:7229)
             at java.lang.reflect.Method.invoke(Native Method)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

When I inspected the response, the url that was returned was changed to : 当我检查响应时,返回的URL更改为:

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22,%22AAPL%22,%22GOOG%22,%222MSFT%22)%26format%3Djson%26diagnostics%3Dtrue%26env%3Dstore%253A%252F%252Fdatatables.org%252Falltableswithkeys%26callback%3D

Thats all that I've been able to find out. 那就是我所能找到的全部。

Change your query to following (key issue was that the original query string you had that also included other query params) 将查询更改为以下(关键问题是您拥有的原始查询字符串还包括其他查询参数)

String query = "select * from yahoo.finance.quote where symbol in (\"YHOO\",\"AAPL\",\"GOOG\",\"MSFT\")";

Note that I also changed retrofit interface to following to test but you can make those other params dynamic if you need 请注意,我也将改造界面更改为跟随以进行测试,但是如果需要,您可以使其他参数动态化

    @GET("v1/public/yql?format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=")
    Call<GetStockResponse> getStock(@Query("q") String query);

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

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