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.
Related
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.
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();
}
I followed this example to parse a local GPX file in Android:
http://android-coding.blogspot.pt/2013/01/get-latitude-and-longitude-from-gpx-file.html
All works fine to access "lat" and "long" but I need also to get the "ele" value but all my tentatives were unsuccessful.
Anyone can give me some hits to do that?
Thanks in advance!
Best regards,
NR.
I will add my library for GPX parsing to these answers: https://github.com/ticofab/android-gpx-parser. It provides two ways to parse you GPX file: once you obtain / create a GPXParser object (mParser in the examples below), you can then either parse directly your GPX file
Gpx parsedGpx = null;
try {
InputStream in = getAssets().open("test.gpx");
parsedGpx = mParser.parse(in);
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
}
if (parsedGpx == null) {
// error parsing track
} else {
// do something with the parsed track
}
or you can parse a remote file:
mParser.parse("http://myserver.com/track.gpx", new GpxFetchedAndParsed() {
#Override
public void onGpxFetchedAndParsed(Gpx gpx) {
if (gpx == null) {
// error parsing track
} else {
// do something with the parsed track
}
}
});
Contributions are welcome.
you have the "Node node = nodelist_trkpt.item(i);" in your first loop.
Get the child elements from this node an run through these child elements.
e.g.:
NodeList nList = node.getChildNodes();
for(int j=0; j<nList.getLength(); j++) {
Node el = nList.item(j);
if(el.getNodeName().equals("ele")) {
System.out.println(el.getTextContent());
}
}
Update: I've added parsing "ele" element as well, so this code could match your requirements.
I will propose different approach: https://gist.github.com/kamituel/6465125.
In my approach I don't create an ArrayList of all track points (this is done in the example you posted). Such a list can consume quite a lot of memory, which can be an issue on Android.
I've even given up on using regex parsing to avoid allocating too many objects (which causes garbage collector to run).
As a result, running Java with 16Mb heap size, parsing GPX file with over 600 points, garbage collector will be run only 12 times. I'm sure one could go lower, but I didn't optimize it heavily yet.
Usage:
GpxParser parser = new GpxParser(new FileInputStream(file));
TrkPt point = null;
while ((point = parser.nextTrkPt()) != null) {
// point.getLat()
// point.getLon()
}
I've successfully used this code to parse around 100 Mb of GPX files on Android. Sorry it's not in the regular repo, I didn't plan to share it just yet.
I've ported the library GPXParser by ghitabot to Android.
https://github.com/urizev/j4gpx
I used TextView#getMaxLines() in my application for a few weeks without incident.
Lint is now informing me that it's only available in API 16+ (#setMaxLines() is API 1+...), though (to the best of my knowledge) I haven't modified anything that would cause this sudden flag - my min-sdk has been 8 for a while, and I have files in my source control to prove it.
1) Why could lint be flagging this error randomly? (To be clear, I mean to say that it should have caught it initially - I'm not implying this is something that it shouldn't have flagged at all).
2) Is there any way to retrieve the maxLines for a TextView on pre-api 16 devices? I checked the source but couldn't devise a way to retrieve this value using the exposed methods on a 2.2 device.
A simpler solution was added to the support lib v4 inTextViewCompat
int maxLines = TextViewCompat.getMaxLines(yourtextView);
Check out this answer for some more informations.
You can use Reflection:
Field mMaximumField = null;
Field mMaxModeField = null;
try {
mMaximumField = text.getClass().getDeclaredField("mMaximum");
mMaxModeField = text.getClass().getDeclaredField("mMaxMode");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
if (mMaximumField != null && mMaxModeField != null) {
mMaximumField.setAccessible(true);
mMaxModeField.setAccessible(true);
try {
final int mMaximum = mMaximumField.getInt(text); // Maximum value
final int mMaxMode = mMaxModeField.getInt(text); // Maximum mode value
if (mMaxMode == 1) { // LINES is 1
text.setText(Integer.toString(mMaximum));
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
OR:
Maybe, the best way is keep maxLine value at values and set it value in xml, and get as int resource in code.
The code for that method simply doesn't exist on 2.2, so you can't use it directly of course.
On the other hand, I've run a diff on the two files and it seems as though the new 4.2.2 TextView isn't using any new APIs internally (this is based solely on its imports). You may be able to add it as a class in your project and use it instead of the inbuilt TextView across all version of Android.
I am downloading a JSONObject from a web site. The entries are however HTML-encoded, using
"
and
&
tags. Is there an easy way to get these to Java strings? Short of writing the converter myself, of course.
Thanks RG
PS: I am using the stuff in a ListView. Probably I can use Html.fromHTML as I can for TextView. Don't know.
OK, I simply went to write my own quick fix. Not efficient, but that's OK for the purpose. A 5-minutes-solution.
public static String unescape (String s)
{
while (true)
{
int n=s.indexOf("&#");
if (n<0) break;
int m=s.indexOf(";",n+2);
if (m<0) break;
try
{
s=s.substring(0,n)+(char)(Integer.parseInt(s.substring(n+2,m)))+
s.substring(m+1);
}
catch (Exception e)
{
return s;
}
}
s=s.replace(""","\"");
s=s.replace("<","<");
s=s.replace(">",">");
s=s.replace("&","&");
return s;
}
I've heard of success in using the Apache Commons on Android.
You should be able to use StringEscapeUtils.unescapeHtml() (from the Lang package).
Here are the (fairly straightforward) directions on using the Apache Commons libraries in your Android apps: Importing org.apache.commons into android applications.