gpt4 book ai didi

java - 如何使远程 api 中的对象可序列化

转载 作者:行者123 更新时间:2023-12-02 11:33:45 25 4
gpt4 key购买 nike

我目前正在进行我的第一个练习,使用服务来处理网络操作。我的第一个应用程序想法涉及使用 YahooFinanceApi https://financequotes-api.com/#singlestock-hist 获取具有一些历史记录的股票报价。我目前遇到的问题是将对象(Stock)返回到要显示的游戏 Activity 。我知道我需要使用可序列化或可打包,但是因为只有当我认为可序列化需要更少的编码时我才会使用它。然而 putExtra 似乎没有将我的 Stock 变量 stockInfo 识别为我可以传输的东西。谁能帮助我理解我做错了什么。

StockPul​​lService 文件编写如下。

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

}
}

游戏 Activity 如下。

公共(public)类 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
}
}

StockPul​​lService 中序列化之前将其包装:

@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();
}

关于java - 如何使远程 api 中的对象可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49094865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com