Importing content from a div of a website into my application

Asked

Viewed 679 times

0

Well I want to take the content of a "div" of a site to display it in my application, I’ve seen something about Webview, but I have no idea how to do...

  • Have you tried jquery? In it there are ways to capture the content of an element by an id and then just put this content in the location you want with jquery itself.

  • I’ve looked and I can’t find the way I want to

1 answer

1


You can use the lib Jsoup for that. An example of yours 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!");
        }
    }
}

Put the permission for internet access

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

With this I take the text inside with id #sites-header-title and put inside a Textview

  • how to insert the jsoup-1.7.3.jar library to my project?

  • download the jar file and place inside the lib folder in your project.

  • I did exactly how you sent me to see if it works, but it was wrong.

  • http://pastebin.com/XEprrd4S

  • Must be the internet permission, I updated the post

  • I thought it was that too is not, already this with permission.

  • Now I found out, I was trying to run 2.2, I tried 4.2 and it worked, only it has to be compatible :(

  • Because there is no way to rotate in 2.2 up? :(

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.