gpt4 book ai didi

android - 如何在android asynctask中使用输入流作为参数?

转载 作者:行者123 更新时间:2023-11-29 01:54:16 25 4
gpt4 key购买 nike

我正在制作一个 android 应用程序来跟踪股票详细信息,我将通过 csv(yahoo finance)检索数据。据我所知,在android 4.0中,网络连接不能在主线程上完成。因此,我将使用 asynctask 来建立连接。但是,我在参数方面遇到了一些问题。请问inputstream类型可以作为参数吗?

public class StockDetails extends Activity {
private InputStream is = null;
private BufferedReader reader = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stock_details);
Intent i = getIntent();
String stockNo = i.getStringExtra(MainActivity.STOCK_NO).toString();
Log.i("Stock No", stockNo);
String strURL = "http://download.finance.yahoo.com/d/quotes.csv?s="+ stockNo +".HK&f=nsl1opc1";
Log.i("URL", strURL);
class HostConnection extends AsyncTask<String, Void, InputStream> {
private Exception ex;

@Override
protected void onPreExecute() {
super.onPreExecute();
}

protected InputStream doInBackground(String... urls) {
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
HttpContext localContext = new BasicHttpContext();
HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
HttpEntity httpEntity = httpResponse.getEntity();
return httpEntity.getContent();
} catch (Exception e) {
this.ex = e;
return null;
}
}
@Override
protected void onPostExecute(InputStream is) {
super.onPostExecute(is);
reader = new BufferedReader(new InputStreamReader(is));
}
}
new HostConnection().execute(strURL);
try {
String line;
while ((line = reader.readLine()) != null){
String[] RowData = line.split(",");
String name = RowData[0];
String symbol = RowData[1];
String currPrice = RowData[2];
String open = RowData[3];
String prevClose = RowData[4];
String change = RowData[5];

TextView stockName = (TextView)findViewById(R.id.stockName);
stockName.setText(name);
TextView stockSymbol = (TextView)findViewById(R.id.stockSym);
stockSymbol.setText(symbol);
TextView stockCurrPrice = (TextView)findViewById(R.id.currPrice);
stockCurrPrice.setText(currPrice);
TextView stockOpen = (TextView)findViewById(R.id.open);
stockOpen.setText(open);
TextView stockPrevClose = (TextView)findViewById(R.id.prevClose);
stockPrevClose.setText(prevClose);
TextView stockChange = (TextView)findViewById(R.id.change);
stockChange.setText(change);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
...
}

}

以上是我的代码,无法执行return httpEntity.getContent();语句,跳转到异常部分。请帮忙。谢谢!

最佳答案

使用 InputStream 作为 Result 值类型是完全合法的,任何其他类也是如此。但是您必须知道 AsyncTask 是异步执行的,因此如果您调用 new HostConnection().execute(strURL); 然后立即尝试使用正在初始化的变量在 AsyncTask 中,您会遇到麻烦。你应该等待 AsyncTask 通过定义某种回调机制来完成它的执行,或者在你的情况下,因为 AsyncTask 是一个内部类,你可以只推送所有BufferedReader相关的代码到onPostExecute()

关于android - 如何在android asynctask中使用输入流作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120234/

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