0

With this code, the application should extract the text of the site div and display it on the screen , but that this did not occur and not [ and presented no error in Logcat , what am I doing wrong ?

    package com.androidbegin.jsouptutorial;

import java.io.IOException;
import java.io.InputStream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends Activity {
    TextView txtdesc;

    // URL Address
    String url = "http://uat.sophiejuliete.com.br/tendencias/";
    ProgressDialog mProgressDialog;

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

        // Locate the Buttons in activity_main.xml
        Button titlebutton = (Button) findViewById(R.id.titlebutton);
        txtdesc = (TextView) findViewById(R.id.desctxt);


        // Capture button click
        titlebutton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // Execute Title AsyncTask
                new Title().execute();
            }
        });

    }


    private class Title extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(MainActivity.this);
            mProgressDialog.setTitle("Android Basic JSoup Tutorial");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();
        }

        @Override
        protected String doInBackground(Void... params) {
            String desc = null;
            try {
                // Connect to the web site
                Document document = Jsoup.connect(url).get();
                // Using Elements to get the Meta data
                Elements description = document.select("div[class=postWrapper]");
                // Locate the content attribute
                desc = description.text();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return desc;
        }

        @Override
        protected void onPostExecute(String result) {
            // Set description into TextView
            txtdesc.setText(result);
            mProgressDialog.dismiss();
        }

    }



}

This is the structure of the site you need to analyze :

<div class="postWrapper" id="post162">
        <div class="postTitle">


            <h2>
                <a href="http://uat.sophiejuliete.com.br/tendencias/agarradinhos-as-orelhas/">
                    Agarradinhos às orelhas                </a>
            </h2>

            <div class="fb-custom-share" data-url="http://uat.sophiejuliete.com.br/tendencias/agarradinhos-as-orelhas/">
                Compartilhar
            </div>

            <div class="date">
                26 de janeiro de 2015            </div>

        </div>

        <div class="postContent"><p>Agarradinhos às orelhas, os solitários e brincos curtos são ideais tanto para o dia como para a noite.</p>
<p>E melhor ainda ficam bem em qualquer formato de rosto.</p>
<p>Basta apenas escolher o modelo conforme a ocasião que você vai utilizar.</p>
<p>&nbsp;</p>
<p><a href="http://sophiejuliete.com.br/shop/brincos.html"><img style="display: block; margin-left: auto; margin-right: auto;" src="http://uat.sophiejuliete.com.br/media/wysiwyg/Agarradinhos_s_orelhas.jpg" alt=""></a></p></div>
    </div>
Paulo Roberto
  • 1,498
  • 5
  • 19
  • 42

1 Answers1

0

Try

desc = description.text();

instead of

desc = description.attr("postContent");

Example:

public static void main(String[] args) throws Exception {
    String url = "http://uat.sophiejuliete.com.br/tendencias/";
    Document document = Jsoup.connect(url).timeout(10000).get();
    // Using Elements to get the Meta data
    Elements description = document.select("div[class=postContent]");
    // Locate the content attribute
    String desc = description.text();
    System.out.println(desc);
    // prints out "Agarradinhos às orelhas, os solitários e brincos..."
}

UPDATE

Since the JSoup part is fixed, you probably have some issue with async task. Try using String as result type, something like this

private class Title extends AsyncTask<Void, Void, String> {

    ...

    @Override
    protected String doInBackground(Void... params) {
        String desc = null;
        try {
            // Connect to the web site
            Document document = Jsoup.connect(url).get();
            // Using Elements to get the Meta data
            Elements description = document.select("div[class=postContent]");
            // Locate the content attribute
            desc = description.text();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return desc;
    }

    @Override
    protected void onPostExecute(String result) {
        // Set description into TextView
        TextView txtdesc = (TextView) findViewById(R.id.desctxt);
        txtdesc.setText(result);
        mProgressDialog.dismiss();
    }

}

UPDATE 2

Declare txtdesc globally, in MainActivity

TextView txtdesc;

initialize it in onCreate()

txtdesc = (TextView) findViewById(R.id.desctxt);

and remove the declaration in onPostExecute(), so there is only txtdesc.setText(result);

@Override
protected void onPostExecute(String result) {
    // Set description into TextView
    txtdesc.setText(result);
    mProgressDialog.dismiss();
}
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • I changed it desc = description.attr ( " postContent " ) ; that is why, desc = description.text (); And yet it did not work ! – Paulo Roberto Feb 24 '15 at 13:51
  • Works for me, maybe you have some other issues with your code but the JSoup part is fine. I've added a small example, can you check if it works for you? – Predrag Maric Feb 24 '15 at 13:55
  • I am newbie on the schedule, how and where I fit in this code snippet , you can position it on my posted code please ? – Paulo Roberto Feb 24 '15 at 13:59
  • This is a sample demo method, doesn't have anything to do with the rest of your project. You can create a new class anywhere in the project and put this method in it (with necessary imports, of course) and run it. If you are using Eclipse IDE, just right click the class, and select `Run As -> Java Application` (similar for any other IDE). The resulting `desc` will be printed in the console. – Predrag Maric Feb 24 '15 at 14:04
  • Understood and performed the procedure and it worked fine , but my doubt still persists , because my project still does not work . – Paulo Roberto Feb 24 '15 at 14:14
  • At least you know that now jsoup part is working. I've edited my answer, try modifying your `AsyncTask` accordingly – Predrag Maric Feb 24 '15 at 14:49
  • It did not work , he brings no data and not the error in logcat – Paulo Roberto Feb 24 '15 at 15:08
  • I've made another edit, in case you already tried the previous one. – Predrag Maric Feb 24 '15 at 16:25
  • I'm about to give up, not even doing that it works and not of any error in logcat – Paulo Roberto Feb 24 '15 at 16:45
  • Try to look for threads similar to this one http://stackoverflow.com/questions/23978400/how-to-update-ui-from-asynctask , I think that's where your problem is. Try to find a basic working demo, and build from there. – Predrag Maric Feb 24 '15 at 17:04