[英]How to make an object from a remote api serializable
我目前正在使用服務處理網絡操作的第一個練習。 我的第一個應用程序想法涉及使用YahooFinanceApi https://financequotes-api.com/#singlestock-hist提取具有一定歷史記錄的股票報價我目前遇到的問題是將對象(股票)返回到要顯示的游戲活動中。 我知道我需要使用可序列化或可打包的,但是因為一旦發現可序列化需要較少的編碼,我就只能使用它。 但是,putExtra似乎無法將我稱為StockInfo的Stock變量識別為可以傳輸的變量。 誰能幫助我了解我在做什么錯。
StockPullService文件的編寫如下。
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import java.io.IOException;
import java.util.Calendar;
import yahoofinance.Stock;
import yahoofinance.YahooFinance;
import yahoofinance.histquotes.Interval;
import static android.content.ContentValues.TAG;
public class StockPullService extends IntentService {
public static final String TICKER = "ticker";
public StockPullService() {
super("StockPullService");
}
@Override
protected void onHandleIntent(Intent workIntent) {
Log.d(TAG, "Service Started");
if(workIntent != null) {
int toMonth = (int) (-1 * Math.ceil(Math.random() * 100)); //Randomises what month to end on a
Calendar from = Calendar.getInstance(); //Makes a calendar from point
Calendar to = Calendar.getInstance(); //Makes a calendar to point
from.add(Calendar.MONTH, toMonth - 5); //From a random number of months ago
to.add(Calendar.MONTH, toMonth); //To 5 months later (To accomodate for 20 weeks of guesses)
try {
Stock stockInfo = YahooFinance.get(workIntent.getStringExtra(TICKER), from, to, Interval.WEEKLY);
Log.d(TAG, "Service Successful");
} catch (IOException e) {
Log.d(TAG, "Service Failed");
e.printStackTrace();
}
Intent intentResponse = new Intent();
intentResponse.addCategory(Intent.CATEGORY_DEFAULT);
intentResponse.putExtra("FetchedStock", stockInfo);
Log.d(TAG, "Service Completed");
}
}
}
並且游戲活動如下。
公共類GameActivity擴展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Intent intent = new Intent(getApplicationContext(), StockPullService.class);
intent.putExtra(StockPullService.TICKER, "HM-B.ST");
startService(intent);
Stock stock = (Stock) getIntent().getSerializableExtra("FetchedStock");
}
}
您可以為Stock
創建一個可序列化的包裝器,如下所示:
public class SerializableStock implements Serializable {
private static final long serialVersionUID = 1L;
private Stock stock;
public SerializableStock(final Stock stock) {
this.stock = stock;
}
public Stock getStock() {
return this.stock;
}
private void writeObject(java.io.ObjectOutputStream stream)
throws IOException {
stream.writeObject(stock.getSymbol());
stream.writeObject(stock.getName());
// TODO: serialize the rest of the fields
}
private void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException {
this.stock = new Stock((String) stream.readObject());
this.stock.setName((String) stream.readObject();
// TODO: deserialize and set the rest of the fields
}
}
在StockPullService
序列化之前將其包裝:
@Override
protected void onHandleIntent(Intent workIntent) {
...
intentResponse.putExtra("FetchedStock", new SerializableStock(stockInfo));
...
}
並解開GameActivity
類:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Stock stock = ((SerializableStock) getIntent().getSerializableExtra("FetchedStock")).getStock();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.