How to implement YQL in Java? [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hey what do I have to do to implement the Yahoo Query Language in Java?
Or is this generally possible?
I want to parse the JSON after that.

YQL is interpreted server-side, so there's not much to do in Java. I'd just make a URL, open it, and read the data stream. Just copy the PHP example code, mostly:
String baseUrl = "http://query.yahooapis.com/v1/public/yql?q=";
String query = "select * from upcoming.events where location='San Francisco' and search_text='dance'";
String fullUrlStr = baseUrl + URLEncoder.encode(query, "UTF-8") + "&format=json";
URL fullUrl = new URL(fullUrlStr);
InputStream is = fullUrl.openStream();
JSONTokener tok = new JSONTokener(is);
JSONObject result = new JSONObject(tok);
is.close();
Depending on what you need, you might want to write some code around the URL construction to make it less messy-looking, and you might like a fancier JSON parser like Gson instead of org.json as I've used here.
You might also get some milage out of a more robust HTTP client library that would allow multiple queries on one connection, etc.

Related

Object oriented programming vs. the split function... which is better? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
here is the non-oop way of doing it:
When someone presses a day on a calendar, we need to find out the day, and path. Here is the non-oop way of storing that information:
A string was created with a "-" delimiter between each piece of info we need, like this:
12-c:\files\john_doe.png
Then it was stored in an array. However, to retrieve the data, we then use the "split" function like this.
for (int t = 0;t < day_and_path.length;t++)
{
String[] day_from_db = day_and_path[t].split("-");
String day_db = day_from_db[0];
String path_db = day_from_db[1];
However the OOP way is, make a class with properties: day, path. Then store them into an array of objects.
Which way is better and why?
If you use String#split, you'll be dealing with an array of strings. If all you do is assign the strings to local variables, then there is no point in making a class for that.
If you want to hold on to the string parts and pass them around to other methods, then it begins to make sense to have an object which will conveniently encapsulate those strings.

Parsing using Jsoup [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I ma new to java.
I have a string s:
s="<name>header</name><content>Good Morning</content>"
How to get value of content and name using Jsoup?
What JSoup returns when content is empty?null?
You can use jsoup:
Connection con2=Jsoup.connect(url);
Document doc = con2.get();
//or use Document doc = Jsoup.parse(html);
Element e=doc.head().select("meta[name=header]").first();
String url=e.attr("content");
http://jsoup.org/cookbook/extracting-data/attributes-text-html
http://jsoup.org/cookbook/extracting-data/selector-syntax
For your edit I do agree with the answer that #Hein give you.
Use this RegEx for example: name=(.*) content=(.*) \/>. The name will be in the first group and the content in the second.
I would recommend double quotes around the strings though. In that case you can use this regex: name=\"(.*)\" content=\"(.*)\"
Edit after the OPs edit:
If you have complete control of the data yourself you should consider saving the name and content in seperate columns in your database, or look into serialization maybe.

Android string manipulation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For this string, 16.82080560, 96.13055810 I want to get
"String one = 16.82080560"
and
"String two = 96.13055810"
in android.
Admin that I suck in string manipulation and regex.
Please let me know how can I get such two value from a string.
String[] components = original.split(",");
If the Strings are always separated by a comma you can use String.split
For a better regex pattern see the comment from #npinti:
Minor side note, it might be better to do \\s*,\\s* instead of just ,.
Just , might cause problems should the OP wish to cast these to
floats, since the extra white space at the beginning of the second
number will most likely not be recognized as a proper number.
Another option:
String str = "16.82080560, 96.13055810";
StringTokenizer st = new StringTokenizer(str,", ");
String one = st.nextToken();
String two = st.nextToken();

How to get a substring from a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this xml file from where I'm reading this string,
http://localhost:8080/sdpapi/request/10/notes/611/
My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?
Split the string
String input = "http://localhost:8080/sdpapi/request/10/notes/611/";
String output = input.split("notes/")[1].split("/")[0];
output is the value you need
What language?
Anyway, in most cases it's a syntax like:
String.substring(begin, length);
... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write
substring(0, 4);
In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of #Liran for example).
// EDIT: In Java the second parameter of substring is not length, but endIndex:
String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));
It depends on programming language you use, but Regular Expressions should be the same in most of them:
/(\d+)\/$/
well, it depend in what language are you writing... in c# for example
string s = #"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));
or
Path.GetFileName(s);
for java
new File(s).getName();

BSON library for java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
We have good support for JSON in java http://blog.locut.us/main/2009/10/14/which-is-the-best-java-json-library.html but what about BSON.
What library do you know that provides BSON support for java?
It should obviously be efficient in runtime.
You can use the MongoDB driver for Java to store a BSON object, then convert that to a String which you can then wrap with JSONObject.
For example, here's how I'll create a regular document:
BasicDBObject obj = new BasicDBObject();
obj.put("name", "Matt");
obj.put("date", new Date());
Then, to get a String representation of the object, simply call:
String bsonString = obj.toString();
Wrap it with a JSONObject and get the date attribute, which should return it in a BSON-compliant format.
JSONObject newObject = new JSONObject(bsonString);
System.out.println(newObject.get("date"));
The resulting output is something like:
{"$date":"2012-08-10T05:22:53.872Z"}
The BSON site is pointing at this
If you want to use it from MongoDB, take a look at this example
In order to get our Model in MongoDB we used google gson to convert our model into JSON first and then we used the JSON util parse method from MongoDB to parse our generated JSON string to a DBObject which you can put in your MongoDB. I don't know about performance to be honest.
There is also a rather new BSON4Jackson project, which allows one to use Jackson for handling BSON data. This means full data binding (to/from POJOs), tree model, even streaming (incremental) reading/writing to degree it can be done with BSON format.
There is also ebson. I've not tried it...

Categories

Resources