我在尝试从多个Yahoo Finance链接中提取股票数据时遇到了问题。基本上我想使用凌空方法一次获得至少4个股票,这意味着一次使用4个不同的URL。对于这个例子,我试图一次至少得到两件事,所以它是
Facebook live stock和Yahoo live stock
但我只是不知道该怎么做而且我收到了一些在队列中创建(添加)新请求的提示,但我已经尝试过它只是不起作用或者我不知道该怎么做它恰当。有人可以帮忙吗?
我正在使用LiveRatesActivity
,CustomJSONObjectRequest
和CustomVolleyRequestQueue
个活动。
主要活动代码:
public class LiveRatesActivity extends AppCompatActivity implements Response.Listener,
Response.ErrorListener {
public static final String REQUEST_TAG = "LiveRatesActivity";
private TextView LastTradePriceOnlyText;
private TextView SymbolText;
private TextView NameText;
private Button GetRates;
private RequestQueue mQueue;
public static final String REQUEST_TAG2 = "LiveRatesActivity";
private TextView LastTradePriceOnlyText2;
private TextView SymbolText2;
private TextView NameText2;
private TextView ChangeText2;
private TextView PercentageChangeText2;
private TextView DaysLowText2;
private TextView DaysHighText2;
private TextView AvgVolumeText2;
private TextView PERatioText2;
private TextView MarketCapText2;
private RequestQueue nQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_rates);
LastTradePriceOnlyText = (TextView) findViewById(R.id.ResultsTextView);
SymbolText = (TextView) findViewById(R.id.SymbolTextView);
NameText = (TextView) findViewById(R.id.NameTextView);
LastTradePriceOnlyText2 = (TextView) findViewById(R.id.ResultsTextView2);
SymbolText2 = (TextView) findViewById(R.id.SymbolTextView2);
NameText2 = (TextView) findViewById(R.id.NameTextView2);
ChangeText2 = (TextView) findViewById(R.id.ChangeTextView2);
PercentageChangeText2 = (TextView) findViewById(R.id.PercentageChangeTextView2);
DaysLowText2 = (TextView) findViewById(R.id.DaysLowTextView2);
DaysHighText2 = (TextView) findViewById(R.id.DaysHighTextView2);
AvgVolumeText2 = (TextView) findViewById(R.id.AvgVolumeTextView2);
PERatioText2 = (TextView) findViewById(R.id.PERatioTextView2);
MarketCapText2 = (TextView) findViewById(R.id.MarketCapTextView2);
GetRates = (Button) findViewById(R.id.Sentbutton);
ImageButton bSettingsBlack = (ImageButton) findViewById(R.id.ibSettingsBlack);
ImageButton bEconCalBlack = (ImageButton) findViewById(R.id.ibEconCalBlack);
ImageButton bNewsBlack = (ImageButton) findViewById(R.id.ibNewsBlack);
bSettingsBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(LiveRatesActivity.this, SettingsActivity.class));
}
});
bEconCalBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(LiveRatesActivity.this, WebViewActivity.class));
}
});
bNewsBlack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(LiveRatesActivity.this, HomePageNews.class));
}
});
}
@Override
protected void onStart() {
super.onStart();
mQueue = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
.getRequestQueue();
String url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22FB%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";//"http://httpbin.org/get?site=code&network=tutsplus";
final CustomJSONObjectRequest jsonRequest = new CustomJSONObjectRequest(Request.Method.GET, url, new JSONObject(), this, this);
jsonRequest.setTag(REQUEST_TAG);
nQueue = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
.getRequestQueue();
String url2 = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";//"http://httpbin.org/get?site=code&network=tutsplus";
final CustomJSONObjectRequest jsonRequest2 = new CustomJSONObjectRequest(Request.Method.GET, url, new JSONObject(), this, this);
jsonRequest2.setTag(REQUEST_TAG2);
GetRates.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mQueue.add(jsonRequest);
nQueue.add(jsonRequest2);
}
});
}
@Override
protected void onStop() {
super.onStop();
if (mQueue != null) {
mQueue.cancelAll(REQUEST_TAG);
}
else if (nQueue !=null) {
nQueue.cancelAll(REQUEST_TAG2);
}
}
@Override
public void onErrorResponse(VolleyError error) {
LastTradePriceOnlyText.setText(error.getMessage());
LastTradePriceOnlyText2.setText(error.getMessage());
}
@Override
public void onResponse(Object response) {
LastTradePriceOnlyText.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("LastTradePriceOnly");
LastTradePriceOnlyText.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
{
SymbolText.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("symbol");
SymbolText.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
NameText.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("Name");
NameText.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
NameText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("Name");
NameText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
SymbolText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("symbol");
SymbolText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
LastTradePriceOnlyText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("LastTradePriceOnly");
LastTradePriceOnlyText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
ChangeText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("Change");
ChangeText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
PercentageChangeText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("PercentChange");
PercentageChangeText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
DaysHighText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("DaysHigh");
DaysHighText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
DaysLowText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("DaysLow");
DaysLowText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
AvgVolumeText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("AverageDailyVolume");
AvgVolumeText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
PERatioText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("PERatio");
PERatioText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
{
MarketCapText2.setText("Response is: " + response);
try {
JSONObject response2 = ((JSONObject) response).getJSONObject("query");
response2 = ((JSONObject) response2).getJSONObject("results");
response2 = ((JSONObject) response2).getJSONObject("quote");
String displayed_Text = ((JSONObject) response2).getString("MarketCapitalization");
MarketCapText2.setText(displayed_Text);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public void NotificationPageRequest(View view) {
Intent intent = new Intent(this, LiveRatesActivity.class);
startActivity(intent);
}
}
请求队列活动:
public class CustomVolleyRequestQueue {
private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
public static CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
mRequestQueue.start();
}
return mRequestQueue;
}
}