2016-12-14 123 views
0

我失去了我需要更改的位置和內容才能使其工作。解析retrofit2響應時出錯

我正在使用this針對雅虎財務的API調用。

這是我的改裝實例:

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

如果我把我的電話:

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

    } 
}); 

接口(YahooApi):

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

基於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; 
    } 
} 

任何有關我需要更改的信息都將非常感謝!

編輯*添加錯誤消息:

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) 

當我檢查了迴應,在返回的網址變更爲:

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 

這就是一切,我已經能夠找出。

+0

你能發佈完整的錯誤,你越來越 –

回答

1

更改查詢到以下(關鍵的問題是,你有這樣的還包括其他的查詢參數的原始查詢字符串)

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

請注意,我也改變了更新接口,以下列測試,但你可以讓那些其他參數動態如果你需要

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

我已經做了你所建議的改變,但無濟於事。如果你有時間看看,我在github上添加了一個鏈接到我的代碼。無論如何,請多多關照! https://github.com/strahinjaajvaz/StockHawk –

+0

遠離筆記本電腦,但你測試了生成的網址(在瀏覽器中)? –

+0

看起來像'查詢'沒有在你的代碼中設置? –