android:如何从互联网上的文本文件填充微调项目?

时间:2013-09-11 15:30:23

标签: android spinner text-files fill

我希望在互联网上的文本文件中填充微调器。 文本文件内容用“,”字符分隔! 我写这段代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn=(Button) findViewById(R.id.btn1);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            readFile();
        }
    });
}
private void readFile()
{
    new Thread() {
        @Override
        public void run() {
            String path ="http://host/file.txt";
            URL u = null;
            try {
                u = new URL(path);
                HttpURLConnection c = (HttpURLConnection) u.openConnection();
                c.setRequestMethod("GET");
                c.connect();
                InputStream in = c.getInputStream();
                final ByteArrayOutputStream bo = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                in.read(buffer); // Read from Buffer.
                bo.write(buffer); // Write Into Buffer.
                String s=bo.toString();

                final Vector<String> str=new Vector<String>();
                String[] line = s.split(",");
                int index = 0;
                while (line[index] != null) {
                    str.add(line[index]);
                    index++;  
                }
runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView text = (TextView) findViewById(R.id.textView1);
                        text.setText(bo.toString());                            
                        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
                        ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, str);
                        spinner.setAdapter(adapter);
                        try {
                            bo.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }.start();
}

用于读取文本文件的读取文件的方法是正确的(我在另一个应用程序中测试)但是用于单独和填充微调器的行不起作用并且强制停止! 帮助我!

0 个答案:

没有答案