Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中从网站url获取html文本?_Android - Fatal编程技术网

如何在android中从网站url获取html文本?

如何在android中从网站url获取html文本?,android,Android,我写了这篇文章,但它不起作用。我也使用了httpclient代码,但这也不起作用。每次执行此函数时,我的模拟器都会停止响应。我不知道问题出在哪里??谁来帮忙!!先谢谢你 打网络电话比较好 使用AsyncTask的方法如下: 在doInBackground()方法中,编写以下代码: public class TestProjMain extends Activity { /** Called when the activity is first created. */ @Overrid

我写了这篇文章,但它不起作用。我也使用了httpclient代码,但这也不起作用。每次执行此函数时,我的模拟器都会停止响应。我不知道问题出在哪里??谁来帮忙!!先谢谢你

打网络电话比较好

使用AsyncTask的方法如下:

  • 在doInBackground()方法中,编写以下代码:

    public class TestProjMain extends Activity {
    /** Called when the activity is first created. */
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            final EditText eText = (EditText) findViewById(R.id.address);
            final TextView tView = (TextView) findViewById(R.id.pagetext);
    
    
            final Button button = (Button) findViewById(R.id.ButtonGo);
            button.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    try {
                        tView.setText("");
                    // Perform action on click
                        URL url = new URL(/*"http://www.google.com"*/eText.getText().toString());
                        URLConnection conn = url.openConnection();
                        // Get the response
                        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        String line = "";
                        while ((line = rd.readLine()) != null) {
    
    
                            tView.append(line);
                        }
                    }
                    catch (Exception e) {
                    }
                }
            });        
        }
    }
    
  • 在onPostExecute()方法中,编写显示代码:

    URL url = new URL(/*"http://www.google.com"*/eText.getText().toString());
    URLConnection conn = url.openConnection();
    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line = "";
    
  • 最好是用它来打网络电话

    使用AsyncTask的方法如下:

  • 在doInBackground()方法中,编写以下代码:

    public class TestProjMain extends Activity {
    /** Called when the activity is first created. */
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            final EditText eText = (EditText) findViewById(R.id.address);
            final TextView tView = (TextView) findViewById(R.id.pagetext);
    
    
            final Button button = (Button) findViewById(R.id.ButtonGo);
            button.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    try {
                        tView.setText("");
                    // Perform action on click
                        URL url = new URL(/*"http://www.google.com"*/eText.getText().toString());
                        URLConnection conn = url.openConnection();
                        // Get the response
                        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        String line = "";
                        while ((line = rd.readLine()) != null) {
    
    
                            tView.append(line);
                        }
                    }
                    catch (Exception e) {
                    }
                }
            });        
        }
    }
    
  • 在onPostExecute()方法中,编写显示代码:

    URL url = new URL(/*"http://www.google.com"*/eText.getText().toString());
    URLConnection conn = url.openConnection();
    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line = "";
    

  • 像这样使用异步任务

            while ((line = rd.readLine()) != null) {
    
    
                tView.append(line);
            }
    
    公共类RetrieveSiteData扩展异步任务{
    @凌驾
    受保护的字符串doInBackground(字符串…URL){
    StringBuilder=新的StringBuilder(16384);
    DefaultHttpClient=新的DefaultHttpClient();
    HttpGet HttpGet=新的HttpGet(URL[0]);
    试一试{
    HttpResponse execute=client.execute(httpGet);
    InputStream内容=execute.getEntity().getContent();
    BufferedReader buffer=新的BufferedReader(新的InputStreamReader(内容));
    字符串s=“”;
    而((s=buffer.readLine())!=null){
    建造商。附加;
    }
    }捕获(例外e){
    e、 printStackTrace();
    }
    返回builder.toString();
    }
    @凌驾
    受保护的void onPostExecute(字符串结果){
    }
    

    }

    像这样使用异步任务

            while ((line = rd.readLine()) != null) {
    
    
                tView.append(line);
            }
    
    公共类RetrieveSiteData扩展异步任务{
    @凌驾
    受保护的字符串doInBackground(字符串…URL){
    StringBuilder=新的StringBuilder(16384);
    DefaultHttpClient=新的DefaultHttpClient();
    HttpGet HttpGet=新的HttpGet(URL[0]);
    试一试{
    HttpResponse execute=client.execute(httpGet);
    InputStream内容=execute.getEntity().getContent();
    BufferedReader buffer=新的BufferedReader(新的InputStreamReader(内容));
    字符串s=“”;
    而((s=buffer.readLine())!=null){
    建造商。附加;
    }
    }捕获(例外e){
    e、 printStackTrace();
    }
    返回builder.toString();
    }
    @凌驾
    受保护的void onPostExecute(字符串结果){
    }
    

    }

    您不能在UI线程上执行此操作。创建一个新线程或服务,获取代码并对其进行解析,然后使用处理程序告诉UI线程您的线程已完成并执行某些操作。他可以更好地使用AsynTask类这就是它没有响应的原因:您是否在清单中添加了internet权限?您不能在UI线程上执行此操作。创建一个新的线程或服务,获取代码并对其进行解析,然后使用处理程序告诉您的UI线程已完成并执行某些操作。他可以更好地使用AsynTask类这就是它没有响应的原因:您是否在清单中添加了internet权限?