Importing the contents of a div from a site into my application

0
Well I want to get the contents of a "div" from a website to display it in my application, I've seen something about WebView, but I have no idea how to do it ...

    
asked by anonymous 06.08.2014 / 03:25

1 answer

1

You can use the Jsoup lib for this. An example of your would be:

MainActivity class

TextView txv;

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

    txv = (TextView) findViewById(R.id.hello);

    new RequestTask().execute("http://sites.ecomp.uefs.br/perta/");
}

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        String text = null;
        try {
            // pega o codigo html de um site
            Document doc = Jsoup.connect(uri[0]).get();
            // pega um elemento do codigo
            Elements newsHeadlines = doc.select("#sites-header-title");
            // pega o texto dentro do codigo
            text = newsHeadlines.text();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return text;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(result != null){
            txv.setText(result);
        }else{
            txv.setText("FALHA!");
        }
    }
}

Place permission for internet access

<uses-permission android:name="android.permission.INTERNET" />

With this I get the text inside with id # sites-header-title and put it inside a TextView

    
06.08.2014 / 05:11