0

我有 1 个 EditText 1Button 和 1TextView,当我在 Edittext 中输入 url 并单击按钮时,textView 将显示来自网站的 Html,我在 edittext 中输入了 url。我想通过使用 url 从 web 获取 html。

问题

当我在(AVD Target 2.3.3 版)中使用此代码时。AndroidManifest (minSdkVersion="10" targetSdkVersion="10") 和我也更改了 targetSdkVersion="15") 两者都正常工作。但是当我将其更改为在(AVD 目标版本 4.0.3)中运行时,它不起作用。是吗?这是我的代码

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

        }
4

3 回答 3

1

当您NetworkOnMainThreadException使用 Honeycomb 或更高版本时,您将无法在 UI 线程上访问网络。您需要在 AsycnTask 中完成您的工作。有关更多信息,请参阅此问题

于 2012-09-08T03:21:00.543 回答
1
    //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

                }

            }
        }
于 2012-09-08T04:41:40.413 回答
0

检查 Menifest 中的 INTERNET PERMISSION。

于 2012-09-08T04:57:48.233 回答