How to use JSOUP to get the data from website - android

I want to get the 'Fixtures' data from this page: [Link] using jsoup but I have no clue of how to get the data.

Include Jsoup in gradle
implementation "org.jsoup:jsoup:1.11.3"
Connect to page
Document doc = Jsoup.connect('url').get();
Select and get the element by id or xpath...
Elements el = doc.getElementsByClass("col");
for (int i = 0; i < el.size(); i++) {
if (el.get(i).classNames().contains("col1")) {
Log.d("EL", el.html());
}
if (el.get(i).classNames().contains("col2")) {
Log.d("EL", el.html());
}
if (el.get(i).classNames().contains("col3")) {
Log.d("EL", el.html());
}
}
p.s. You will need to handle asnyc call yourself Jsoup.connect will throw NetworkOnMainThreadException if you call it directly in activity.

Related

PDFTron iOS and Android how to open the PdfViewCtrl at a specific page?

We are using the PDFTron SDK to read PDFs in our Xamarin app.
What we want to do is open the PDF at a specific page, since we want our users to continue reading where they left on our website.
We are following the example found here, with the PTTabbedDocumentViewController on iOS. Here is what we tried, to make this work.
PDFDoc pdfDoc = TypeConvertHelper.ConvPdfDocToManaged(mTabViewController.SelectedViewController.PdfViewCtrl.GetDoc());
if (pdfDoc != null)
{
var pageCount = pdfDoc.GetPageCount();
}
But, the pdfDoc instance is always null. Please, can someone help?
Android
There is a method to start the viewer on a specific page:
https://www.pdftron.com/api/xamarinandroid/pdfnet/api/pdftronprivate.PDF.PDFViewCtrl.html#pdftronprivate_PDF_PDFViewCtrl_CurrentPage:
​
Here is a code sample:
var myPage = 3;
mPdfViewCtrl.DocumentLoad += (sender, e) =>
{
// On document loaded, set the page
mPdfViewCtrl.CurrentPage = myPage;
};
iOS:
For iOS the equivalent method is this:
https://www.pdftron.com/api/xamarinios/tools/api/pdftron.PDF.PTPDFViewCtrl.html#pdftron_PDF_PTPDFViewCtrl_SetCurrentPage_System_Int32_
mPdfViewCtrl.OnSetDoc += (sender, e) =>
{
mPdfViewCtrl.SetCurrentPage(3);
};

Is there a transformItems equivalent in the Android Java Libraries for Algolia?

I have a use case where i would like to render the image associated with the hits returned from an Algolia search using the Algolia Java library for Android. I am currently developing on Pie . Here is what i am doing :
I use com.algolia.instantsearch.core.helpers.Searcher
I bind the results to a fragment which has a layout with the algolia attributes for images
<ImageView
algolia:attribute='#{"image_url"}'
>
The trouble is that the response JSON only stores the name of the JPG image which needs to be displayed. I need to dynamically add the base site URL and some more path specifiers . I tried doing something like this
algolia:attribute='https://somedomain.com/somepath1/ProductImages/#{"BaseProductId"}/thumbnails/#{"image_url"}
But that was not accepted.
I am looking for a way to transform the results so i can build the complete URL and place it in the image_url and then use it in the layout as stated in the first code fragment.
Is there any way to do it ?
I solved it by adding a listener and updating the hits object as seen below.
searcher.registerResultListener(new AlgoliaResultsListener() {
#Override
public void onResults(#NonNull SearchResults results, boolean isLoadingMore) {
for (int i=0;i<results.hits.length();i++){
try {
JSONObject obj = results.hits.getJSONObject(i);
String image_url_file = obj.getString("image_url");
String base_product_id = obj.getString("BaseProductId");
String full_image_path = "https://somedomain.com/somPath/ProductImages/"+base_product_id+"/Original/"+image_url_file;
results.hits.getJSONObject(i).put("image_url",full_image_path);
}catch(Exception exx){
}
}
}
}
);

Convert html parser with multiple divs from swift to android using Jsoup

I am trying to convert iOS application into android. But I just start learning Java a few days ago. I'm trying to get a value from a tag inside html.
Here is my swift code:
if let url = NSURL(string: "http://www.example.com/") {
let htmlData: NSData = NSData(contentsOfURL: url)!
let htmlParser = TFHpple(HTMLData: htmlData)
//the value which i want to parse
let nPrice = htmlParser.searchWithXPathQuery("//div[#class='round-border']/div[1]/div[2]") as NSArray
let rPrice = NSMutableString()
//Appending
for element in nPrice {
rPrice.appendString("\n\(element.raw)")
}
let raw = String(NSString(string: rPrice))
//the value without trimming
let stringPrice = raw.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
//result
let trimPrice = stringPrice.stringByReplacingOccurrencesOfString("^\\n*", withString: "", options: .RegularExpressionSearch)
}
Here is my Java code using Jsoup
public class Quote extends Activity {
TextView price;
String tmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote);
price = (TextView) findViewById(R.id.textView3);
try {
doc = Jsoup.connect("http://example.com/").get();
Element content = doc.getElementsByTag("//div[#class='round-border']/div[1]/div[2]");
} catch (IOException e) {
//e.printStackTrace();
}
}
}
My problems are as following:
I got NetworkOnMainThreatException whenever i tried any codes.
I'm not sure that using getElementByTag with this structure is correct.
Please help,
Thanks.
I got NetworkOnMainThreatException whenever i tried any codes.
You should use Volley instead of Jsoup. It will be a faster and more efficient alternative. See this answer for some sample code.
I'm not sure that using getElementByTag with this structure is correct.
Element content = doc.getElementsByTag("//div[#class='round-border']/div[1]/div[2]");
Jsoup doesn't understand xPath. It works with CSS selectors instead.
The above line of code can be corrected like this:
Elements divs = doc.select("div.round-border > div:nth-child(1) > div:nth-child(2)");
for(Element div : divs) {
// Process each div here...
}

What type of JSON parsing using volley I should use?

I'm using Volley and looking at this ( http://www.androidhive.info/2014/09/android-json-parsing-using-volley/ ) tutorial, but I don't know how to make it work. Using ObjectJSON, error says "it can't be converted to Array" and if I use ArrayJSON method it doesn't found database elements.
My urlJSON - http://smkbaig.esy.es/get_info_test.php
Your JSON following the php link you provided starts with { and as the tutorial said, that's a JSON Object, followed by an array called "receptai".
If you have followed the tutorial correctly till the end, it should work using
makeJsonArrayRequest()
You really need to paste your code here so that we could help further.
What you might want to do first is follow the tutorial exactly the way it was presented, and if you get responses successfully, then start experimenting and changing. I see you are using your own JSON instead of coding for both JsonArrays and JsonObjects and seeing both buttons get functional.
Thnaks #iBobb for answer, it helped me.
Here is how it worked out:
try {
JSONArray ja = response.getJSONArray("receptai");
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject = ja.getJSONObject(i);
rec = new Receptas();
rec.setPav(jsonObject.getString("pav"));
rec.setApras(jsonObject.getString("apras"));
rec.setIngred_sk(jsonObject.getString("ingred_sk"));
recList.add(rec);
}
// ListView
// txtResponse.setText(data);
} catch (JSONException e) {
e.printStackTrace();
}

Problem with extracting data from xml

I want to extract data from xml below. rate & currency works fine but i could not extract time.
<Cube>
<Cube time='2011-04-15'>
<Cube currency='USD' rate='1.4450'/>
<Cube currency='JPY' rate='120.37'/>
</Cube>
The code in startElement method
if (localName.equals("Cube")) {
for (int i = 0; i < attributes.getLength(); i++) {
if ((attributes.getLocalName(i)).equals("currency")) {
name = attributes.getValue(i);
} else if ((attributes.getLocalName(i)).equals("time")) {
date = attributes.getValue(i);
}
else if ((attributes.getLocalName(i)).equals("rate")) {
try {
rate = Double.parseDouble(attributes.getValue(i));
} catch (Exception e) {
}
Annotation based parsers are quite nice at this sort of work and I think that the Simple XML library can handle this for you. You should check it because it may meet your needs in a much better way.
I even wrote a blog post on how to use it in one of your android projects: which you can find here.

Categories

Resources