Daniel Daniel - 8 days ago 5
Android Question

JSOUP html parse giving different results on android device vs windows machine

The following code ran on a java file on my computer gives the correct result of "/pws/client/pdf/offers-in-store-10-11-16.pdf"

String pdfLink= null;
try {


Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").get();
Element links = doc.select("a[title=\"Download offers in store\"]").first();
System.out.println(links.attr("href"));


} catch (IOException e) {
//e.printStackTrace();
}


However when I run the code on my android device in an app where it is activated by the button press,I get a null pointer exception on "pdfLink=links.attr("href");" so it isnt finding the link "/pws/client/pdf/offers-in-store-10-11-16.pdf" for whatever reason.In my android app the code is on a onclick listener of a button and I have stepped through the code,it does activate the on click and runs the right lines of code but for whatever reason JSOUP is not finding the link.Below is the android code

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnFetchData = (Button) findViewById(R.id.buttonTest);
btnFetchData.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
new FetchWebsiteData().execute();


}
});

}
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {

private String pdfLink = "didnt work";


@Override
protected void onPreExecute() {
super.onPreExecute();


}

@Override
protected Void doInBackground(Void... params) {
try {

Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").get();
//Elements links = doc.select("a[title=\"Download offers in store\"]");
Element links = doc.select("a[title=\"Download offers in store\"]").first();
pdfLink=links.attr("href");


} catch (IOException e) {
//e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
TextView txttitle = (TextView) findViewById(R.id.resultTextView);
txttitle.setText(pdfLink);

}

}

}

Answer

the mobile browser user-agent differs from the desktop browser; therefore, the HTML responses differ. In order to get the same result you have to set a desktop user-agent. Change this line :

Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").get();

to:

Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();
Comments