-1

I am doing an android app for a university project. I want to read in the text from two websites using jsoup. I am getting an error when creating the document from url using jsoup:

"Error:(50, 227) error: incompatible types: org.jsoup.nodes.Document cannot be converted to org.w3c.dom.Document"

Have watched so many tutorials on this but it does not seem to be an issue for anybody else. Would be so grateful for some help to figure out why its not working!

Here is my code:

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import org.jsoup.Jsoup;
import org.w3c.dom.Document;


import java.io.FileWriter;
import java.io.PrintWriter;

public class MainActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new getData().execute();
        }
    });
}

public class getData extends AsyncTask<Void, Void, Void>{
    String avgT;
    String avgRain;
    String fileNameR = "Rainfile.txt";
    String fileNameT = "Tempfile.txt";


    @Override
    protected Void doInBackground(Void... params) {
        Document temp;
        Document rain;
        try {
            temp = Jsoup.connect("http://www.timeanddate.com/weather/singapore/singapore/historic").get(); //avg temp
            avgT=temp.getTextContent();
            FileWriter fw1 = new FileWriter(fileNameT);
            PrintWriter pw1 = new PrintWriter(fw1);
            pw1.println(avgT);
            pw1.close();

            rain = Jsoup.connect("http://www.weatheronline.co.uk/weather/maps/city?LANG=en&WMO=48698&ART=PRE&CONT=asie&R=0&LEVEL=150&REGION=0027&LAND=SX&NOREGION=0&MOD=&TMX=&TMN=&SON=&PRE=&MONAT=&OFFS=&SORT=").get();
            avgRain=rain.getTextContent();
            FileWriter fw2 = new FileWriter(fileNameR);
            PrintWriter pw2 = new PrintWriter(fw2);
            pw2.println(avgRain);
            pw2.close();
        }catch (Exception e){e.printStackTrace();} //http://www.timeanddate.com/weather/singapore/singapore/historic
        return null;
    }



}

}

`

  • you need to read Tutorials for this – Amitsharma Oct 17 '16 at 12:59
  • http://stackoverflow.com/questions/31552242/sending-http-post-request-with-android , http://stackoverflow.com/questions/2559948/android-sending-xml-via-http-post-soap , http://stackoverflow.com/questions/15425736/android-creating-and-sending-an-xml-soap-request-programmatically – Amitsharma Oct 17 '16 at 13:00
  • 2
    java's basics: Error is obvious ... `Jsoup.connect(..)` returns `org.jsoup.nodes.Document` not `org.w3c.dom.Document` ... `temp` and `rain` are declared as the second type .... – Selvin Oct 17 '16 at 13:12

1 Answers1

2

you are importing a wrong Document type. Change this line

import org.w3c.dom.Document; 

to

import org.jsoup.nodes.Document;
Eritrean
  • 15,851
  • 3
  • 22
  • 28