Error when get HTML from web in android

2019-03-04 16:28发布

问题:

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

        }

回答1:

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.



回答2:

    //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

                }

            }
        }


回答3:

Check INTERNET PERMISSION in Menifest.