如何使用InputStream的作为Android的AsyncTask的PARAMS? [英] how to use inputstream as params in android asynctask?

查看:132
本文介绍了如何使用InputStream的作为Android的AsyncTask的PARAMS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个Android应用程序来跟踪股票的细节,我会通过CSV(雅虎财经)来检索数据。据我所知,在Android 4.0的,网络连接不能在主线程中完成的。因此,我将使用的AsyncTask进行连接。但是,我面对一些问题的PARAMS。我想问一下,可以InputStream的使用类型为PARAMS?

I am making an android application to track stock details and i am going to retrieve the data through csv (yahoo finance). As I know, in android 4.0, network connection can't be done on the main thread. Therefore, I am going to use asynctask to make the connection. However, i face some problems with the params. I would like to ask that can inputstream type use as params?

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) {
        ...
    }

}

以上是我的code,它不能执行该语句返回httpEntity.getContent(); 跳进异常的一部分。请帮忙。谢谢!

The above is my code and it can't execute the statement return httpEntity.getContent(); and jump into the exception part. Please help. Thanks!

推荐答案

使用的InputStream 的结果值类型是完全合法的,以及其他类。但是,你必须意识到,一个的AsyncTask 异步执行的,因此,如果调用新HostConnection()执行(strURL); 然后立即尝试使用正在里面的的AsyncTask ,你会遇到麻烦初始化的变量。你应该等待的AsyncTask 通过定义某种回调机制,或在您的情况下,作为的AsyncTask 是一个内部类,你可以推凡是涉及到的BufferedReader onPostExecute()。

Using InputStream as the Result value type is perfectly legal, as well as any other class. But you must be aware that an AsyncTask executes asynchronously, so if you call new HostConnection().execute(strURL); and then immediately try to use variables that are being initialized inside the AsyncTask, you're gonna be in trouble. You should wait for the AsyncTask to finish its execution by defining some kind of callback mechanism, or in your case, as the AsyncTask is an inner class, you can just push all the code related to BufferedReader to the onPostExecute().

这篇关于如何使用InputStream的作为Android的AsyncTask的PARAMS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆