错误:android.os.NetworkOnMainThreadException

时间:2015-08-16 16:55:44

标签: java android eclipse

我制作一个简单的Android应用程序。现在我做了一个活动,它从网上加载信息并使用这些信息来设置列表视图。如果运行应用程序它工作正常,但如果我打开活动(我上面描述)他给出错误:android.os.NetworkOnMainThreadException

活动档案:

package com.a3gaatleren;

import android.support.v7.app.ActionBarActivity;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.a3gaatleren.ReadFile;

public class Agenda extends ActionBarActivity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_agenda);

                doInBackground();

        }



        protected void doInBackground(){
                try {
                        ListView mainListView ;  
                        // Find the ListView resource.     
                        mainListView = (ListView) findViewById( R.id.listView2);
                        String file_name =  "http://3gaatleren.16mb.com/appagenda/agenda.html";

                        // Create and populate a List of planet names.
                        ReadFile file = new ReadFile(file_name);

                        String[] huiswerk = file.OpenFile();
                        ArrayList<String> huiswerklist = new ArrayList<String>();  
                        huiswerklist.addAll( Arrays.asList(huiswerk) );  

                        // Create ArrayAdapter using the planet list. 
                        ArrayAdapter<String> listAdapter;
                        listAdapter = new ArrayAdapter<String>(Agenda.this, R.layout.simplerow, huiswerklist);  

                        // Set the ArrayAdapter as the ListView's adapter.  
                mainListView.setAdapter( listAdapter ); 

                        }catch(MalformedURLException e){
                                System.out.println(e);

                        }catch(IOException f){
                                System.out.println(f);
                        }

        }





        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.agenda, menu);
                return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                        return true;
                }
                return super.onOptionsItemSelected(item);
        }

}

我定义readfile函数的java文件:

package com.a3gaatleren;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Scanner;

import org.jsoup.Jsoup;

import java.io.FileReader;
import java.io.BufferedReader;

public class ReadFile {

        private String path;

        public ReadFile (String file_path){
                path= file_path;
        }


        public String[] OpenFile() throws IOException{

                URL url = new URL(path);
                BufferedReader textReader = new BufferedReader(new InputStreamReader(url.openStream()));
                int numberOfLines=readLines();
                String[] textData = new String[numberOfLines];

                int i;

                for (i=0; i< numberOfLines; i++){
                        String html = textReader.readLine();
                        org.jsoup.nodes.Document doc;
                        doc = Jsoup.parse(html, "utf-8");
                        String text = doc.body().text();

                        textData[i] =text;
                }

                textReader.close();
                return textData;

        }

        int readLines() throws IOException{
                URL file_to_read = new URL(path);
                BufferedReader bf = new BufferedReader(new InputStreamReader(file_to_read.openStream()));

                int numberOfLines = 0;

                String str;

                while ((str = bf.readLine()) != null){
                        numberOfLines++;

                }
                bf.close();

                return numberOfLines;


        }

}

有人可以告诉我这是什么问题吗?

PS:英语不是我的第一语言,我不知道它是否是正确的英语

1 个答案:

答案 0 :(得分:0)

基本上,UI线程是您应用程序的一部分,用于处理屏幕上显示的内容并让用户使用它。如果用户尝试某些内容时没有响应,那么用户认为应用程序运行缓慢,最糟糕的情况是用户认为应用已崩溃且Android不好,这是我们不想要的。

为了解决这个问题,我们可以在另一个线程上做任何需要很长时间或大量处理能力的事情。最简单的方法是使用AsyncTask。

new AsyncTask<Void, Void, Void>() {

    public void doInBackground(Void... params,) {
        //do your loading here
        return null;
    }

    public void onPostExecute(Void result) {
        //update your ui here when loading finishes
    }

}.execute();

以上是绝对的骨头,但应该可以帮助您解决问题