Error when get HTML from web in android

2019-03-04 16:58发布

I have 1 EditText 1Button and 1TextView, when I type url in Edittext and click button, the textView will be show the Html from website that i type the url in edittext. I want to get html from web by using url.

Problem

When I using this code in ( AVD Target version 2.3.3). AndroidManifest (minSdkVersion="10" targetSdkVersion="10") and I also change targetSdkVersion="15") both are work correct. but when I change it to run in (AVD target version 4.0.3) it's not work. Y? This is my code

    final EditText et = (EditText) findViewById(R.id.editText1);
    final Button b = (Button) findViewById(R.id.button1);
    final TextView tv = (TextView) findViewById(R.id.textView1);

    b.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
        try {
            URL url = null;
            url = new URL(et.getText().toString());
            URLConnection conn = url.openConnection();
            BufferedReader buff = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line ="";
            while((line = buff.readLine())!= null){
                tv.append(line);

            }
        } catch (Exception e) {

        }

3条回答
姐就是有狂的资本
2楼-- · 2019-03-04 17:07

Check INTERNET PERMISSION in Menifest.

查看更多
疯言疯语
3楼-- · 2019-03-04 17:24
    //before OnCreate() method
    URL url = null;
     final TextView tv;
    ////////


     b.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
            try {
                url = new URL(et.getText().toString());

    new YourAsyncTask.execute();
                }
            } catch (Exception e) {

            }


    //after onCreate() method

     class YourAsyncTask extends AsyncTask<Void, Void, Void>
        {

            private ProgressDialog progressDialog;

            @Override
            protected void onPreExecute()
            {
                //show your dialog here
                progressDialog = ProgressDialog.show(YourActivity.this,"Please wait...", "Loading  ...", true);
            }

            @Override
            protected Void doInBackground(Void... params)
            {  


                //make your request here - it will run in a different thread
                try
                {

                URLConnection conn = url.openConnection();
                BufferedReader buff = new BufferedReader(new  InputStreamReader(conn.getInputStream()));
                String line ="";
                while((line = buff.readLine())!= null){
                    tv.append(line);




                }
                catch (Exception e)
                {
                    // TODO: handle exception
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result)
            {

                try
                {   
                    progressDialog.dismiss();
///Show your Data here

                }
                catch (Exception e)
                {
                    // TODO: handle exception

                }

            }
        }
查看更多
劳资没心,怎么记你
4楼-- · 2019-03-04 17:29

You're getting a NetworkOnMainThreadException, you can't access the network on the UI thread when using Honeycomb or later. You need to do your work in an AsycnTask. See this question for more info.

查看更多
登录 后发表回答