Trouble with getElemenstByClass() function in JSoup - android

I'm writing an app that fetches text sms from html of website smsmaza.in, for which I'm using Jsoup to parse HTML. Following is the code which is troubling me
BLOG_URL="http://www.smsmaza.in/";
Document document;
document = Jsoup.connect(BLOG_URL).timeout(12000).get();
Elements texts=document.getElementsByClass("sms");
When I print value of texts.size() it comes to be zero, which means nothing is selected. What is the problem?
Thanks in advance.
Here is the complete program :- http://pastecode.org/index.php/view/20317090

from your code i have used:
Document document=Jsoup.connect("http://www.smsmaza.in/").timeout(12000).get();
Elements texts=document.getElementsByClass("sms");
Log.e("sms", Integer.toString(texts.size()));
and logcat show me 10 sms classes are selected. so it is working well.
you should not block setContentView. and in your bellow code:
if(texts.size()>0){
int i=0;
while(i<texts.size()){
result[i]=texts.get(i).text();
//you should increase your i here
}
}
you should increase i++ in while loop.
if it doesn't help, try this:
int i = 0;
for(Element element : texts){
result[i] = element.text();
i++;
}

Related

Converting HTML escape sequence to Java

I am receiving a string using REST APIs.
JSONObject obj = new JSONObject(output);
JSONArray contacts = obj.getJSONArray("results");
JSONObject result = contacts.getJSONObject(0);
..
String brandName = result.getString("productName");
In some cases productName comes as Dri-FIT™ Element Half Zip.
I want to show it as "Dri-FIT™ Element Half Zip" in Android, but it shows up in the TextView as Dri-FIT™ Element Half Zip.
Can anyone help me as to how to convert the HTML escape sequence to a valid Java escape sequence so that I can view it?
You can try this. I think this is the best way to show your special symbol.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tv.setText(Html.fromHtml("Dri-FIT™ Element Half Zip", Html.FROM_HTML_MODE_COMPACT));
}else{
tv.setText(Html.fromHtml("Dri-FIT™ Element Half Zip"));
}
Please replace &#8482 to \u2122 (2122 is hex for 8482)

How to catch a View with Tag by Espresso in Android?

I have a PinCodeView that extends LinearLayout. I have following code in my init() method. DigitEditText extends EditText and just accepts one digit. This view will be used to receive confirmation code which has 4 digits long.
private void init()
{
...
for (int i = 0; i < 4; i++)
{
DigitEditText digitView = getDigitInput();
digitView.setTag(R.id.etPinCodeView, i); // uses for Espresso testing
digitView.setKeyEventCallback(this);
...
}
I have created res/values/ids.xml and this is its content:
<resources>
<item name="etPinCodeView" type="id"/>
</resources>
Now, in Espresso I want to catch each DigitEditText and put a digit in it. How I'm able to do that? I see there are two methodes, withTagKey() and withTagValue() but I have no idea how to get them into work.
I thought something like this might work but seems I'm not able to assign 0 into withTagValue().
onView(allOf(withTagKey(R.id.etPinCodeView), withTagValue(matches(0)))).perform(typeText("2"));
Since withTagValue needs an instance of org.hamcrest.Matcher as an argument, we can create a simple one using the Matcher.is method
to find views with a certain tag in your expresso test:
String tagValue = "lorem impsum";
ViewInteraction viewWithTagVI = onView(withTagValue(is((Object) tagValue)));
After setting the tag in your view somewhere in the app, for those confused about the syntax in Kotlin:
withTagValue(`is`(EXPECTED_TAG))
The complete syntax to assert a tag on a specific view:
onView(
allOf(
withId(R.id.some_id),
withTagValue(`is`(EXPECTED_TAG))
)
)
Simple :)
I solved my problem with this trick. Hope it saves some times for you.
First I used Id rather than tag.
for (int i = 0; i < 4; i++)
{
DigitEditText digitView = getDigitInput();
digitView.setId(R.id.etPinCodeView + i); // uses for Espresso testing
digitView.setKeyEventCallback(this);
...
And this is test for it:
onView(withId(R.id.etPinCodeView + 0)).perform(typeText("2"));
onView(withId(R.id.etPinCodeView + 1)).perform(typeText("0"));
onView(withId(R.id.etPinCodeView + 2)).perform(typeText("1"));
onView(withId(R.id.etPinCodeView + 3)).perform(typeText("6"));

How to get text out of TextView or ListView Android?

I have a ListView. This ListView load this text/data from a URL/HTML code on a webpage. I use a for loop for it like:
for (int i = 0; i < 5; j++) {
// Search and load text in the ListView..
}
But sometimes the webpage has 5 "textfields" but maybe a new post got 8..
So, I don't want to use the 5 in the for loop anymore.. I want a for loop which is loading and loading untill he find a specific line in the html code of the webpage.
For example:
if (MaxLoad != "<p>End of the textfields</p>") {
// Search and load text in the ListView,
// untill the found text is the text between the "".
}
}
else{
Log.e("Max textfields are found!")
}
Sometimes he need to stop after 3 textfields.. But another time he need to stop after 16 textfields..
I hope I was clear enough.
Thanks,
P.S. All my code is working at the moment.. When I use the for loop system, count the textfields in the HTML manually.. Put that value into the for loop, then the code load all the textfields.. But I want it automaticly..
Use the break; statement to break out of your for loop. You can initiate the for loop with a big number like 5000.
for (int i = 0; i < 5000; j++) {
// Search and load text in the ListView..
String ItemText = .......
if ( ItemText.equals ( "blablabla" ) )
break;
}
This could be done more elegant though...

Android Jsoup in service - get text of span

Im pretty new to jsoup. For days im trying now to read out a simple number from a span without any success.
I hope to find help here. My html:
<div class="navi">
<div class="tab mail">
<a href="/comm.php/indexNew/" accesskey="8" title="Messages">
<span class="tabCount">1 </span>
<img src="/b2/message.png" alt="Messages" class="moIcon i24" />
</a>
</div>
The class tabCount excists 3 times though in the whole document and I am interested in the first span with this class.
Now I am trying in onCreate() of a service to create a thread with:
Thread downloadThread = new Thread() {
public void run() {
Document doc;
try {
doc = Jsoup.connect("https://www.bla.com").get();
String count = doc.select("div.navi").select("div.tab.mail").select("a[href]").first().select("tabCount").text();
Log.d("SOMETHING", "test"+(count));
} catch (IOException e) {
e.printStackTrace();
}
}
};
downloadThread.start();
This forces my app to crash. The same if i change text() to ownText(). if i remove text() then the app can start but it gives me null.
what am i doing wrong? By the way, besides the service a webview is loading the same url. might that be a problem?
You only need to select the element you're interested in, you don't need to get every outer element before. In your example you could try
String count = doc.select("span.tabCount").text();
Where you define the type of the element "span" and class name ".tabcount"
For an example that might help you, look at this link
Edit:
Try this code instead, this will get the value of the first span.
Elements elements = doc.select("span.tabCount");
String count = elements.first().text();
And if you want to print all elements you could do like this.
Elements elements = doc.select("span.tabCount");
for (Element e : elements) {
Log.d("Something", e.text();
}
Haven't you meant .select(".tabCount")?
BTW, on Android AsyncTasks are more convenient than Threads. Also, empty catch blocks are a bad practice.
Your select statement is wrong. You can insert the whole selection string in one line. Furthermore you have to prefix "tabCount" with a dot as it is a class.
String count = doc.select("div.navi div.tab.mail a").first().select(".tabCount").text();

Parsing problem in android

I have create an android application where parse data using saxparser .But Problem is that it does not parse the whole string inside the tag. The first 4 or 5 word is shown. Why i do not able to parse the whole string inside the tag. I follow the site....
http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser
Please help me . Thank in advance.
Use this in the character method of your xml handler
public void characters (char ch[], int start, int length) {
if (buf!=null) {
for (int i=start; i<start+length; i++) {
buf.append(ch[i]);
}
}
}
Where buf is a string builder.
The tutorial you followed has a bug in their characters method. The characters method may be called multiple times per tag, so you need to append the characters to your current value rather than starting a new String.

Categories

Resources