android - How can I construct this URL for RetroFit API -
this url using query results:
http://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.historicaldata+where+symbol+%3d+%22yhoo%22+and+startdate+%3d+%222015-11-10%22+and+enddate+%3d+%222016-11-10%22&format=json&diagnostics=true&env=store%3a%2f%2fdatatables.org%2falltableswithkeys&callback=
i using retrofit first far how build url:
public static string getstockdataurl(string stock_symbol){ string startdate = utils.getlastyear() ; string enddate = utils.gettodaydate(); try{ string yahoo_base_url = constants.yahoo_base_query; string query_stock_data = constants.query_stock_data + constants.symbol_query +stock_symbol+ constants.start_date_query +startdate+"\" " + constants.end_date_query + enddate+"\""; return yahoo_base_url + urlencoder.encode(query_stock_data, "utf-8") + constants.format_query + constants.tables_callback_query; }catch (exception e){ e.printstacktrace(); } return null; }
these constants used create url
public static final string yahoo_base_query = "http://query.yahooapis.com/v1/public/yql?q="; public static final string query_stock_data = "select * yahoo.finance.historicaldata "; public static final string symbol_query = "symbol = \""; public static final string start_date_query = "\" , startdate = \""; public static final string end_date_query = "and enddate = \""; public static final string format_query = "&format=json&diagnostics=true&env=store%3a%2f%2fdatatables."; public static final string tables_callback_query = "org%2falltableswithkeys&callback=";
i have stock_symbol input user , create url last year date , todays date how can achieve retrofit api interface ?
this method query data retrofit
public void getstockquotes(string symbol) { quotesapi apiservice = apiclient.getclient().create(quotesapi.class); string query = utils.getstockdataurl(symbol); log.d(log_tag, query); call<quotesresponse> call = apiservice.getquotes(query); call.enqueue(new callback<quotesresponse>() { @override public void onresponse(call<quotesresponse> call, retrofit2.response<quotesresponse> response) { list<quotes> movies = response.body().getresults(); log.d(tag, "number of movies received: " + movies.size()); } @override public void onfailure(call<quotesresponse> call, throwable t) { log.d(tag, t.tostring()); } });
this quotesapi interface have created
@get call<quotesresponse> getquotes(@url string url);
these logcat details:
java.lang.nullpointerexception: attempt invoke virtual method 'java.util.list com.android.stockhawk.quotes.quotesresponse.getresults()' on null object reference @ com.android.stockhawk.service.stocktaskservice$1.onresponse(stocktaskservice.java:175) @ retrofit2.executorcalladapterfactory$executorcallbackcall$1$1.run(executorcalladapterfactory.java:68) @ android.os.handler.handlecallback(handler.java:746) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5443) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:728) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:618)
an interface should declared when using retrofit.
public interface stockdataservice {}
in interface, declare method:
// string int @get annotation should subpath , should declare baseurl @get("http://query.yahooapis.com/v1/public/yql") call<result> access(@querymap map<string, string> options);
put string "q", "format", "diagnostics","env","callback" keys of options
actual values values of options
Comments
Post a Comment