I'm trying to write a code to get the latitude and longitude of a user inputted city.
I've written a code which searches a website and then stores its HTML data in a string.
I now want to search the string(the HTML of the website) for the values.
I've looked into API's, and HTML parsers, but they are all too complicated for me (I'm still in school, just starting out), so please don't recommend those unless its absolutely impossible to do without them.
Code:
import java.net.*;
import java.io.*;
import java.util.Scanner;
import static java.lang.System.*;
class websearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//gets the city
out.println("enter city, add plus between multiple words");
String term = sc.nextLine();
try {URL url = new URL("http://www.geonames.org/search.html?q=" + term + "&country=");
URLConnection ucl = url.openConnection();
InputStream stream = ucl.getInputStream();
int i;
//the string in which the html code will be stored
String code = " ";
while ((i=stream.read())!= -1) {
code += Character.toString((char)i);
}
//printing the html, only for testing
System.out.print(code);
} catch(Exception e) {
System.out.println("error");
}
}
}
This code prints a string too large to be pasted here, but the values I want to find look like this:
<td nowrap="">N 40° 42' 51''</td>
<td nowrap="">W 74° 0' 21''</td>
How could I find this sequence of characters, and then store only the latitude and longitude in a variable?
This tutorial should be of some use to you. it goes over how to get the geocode data without actually using the google maps API. If you follow what it says, you should have a fairly easy time implementing it.
The end result is that you will enter the street address of the location you want, and it will return the latitude and longitude in searchable variables.
Primary working class
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
public class OpenStreetMapUtils {
public final static Logger log = Logger.getLogger("OpenStreeMapUtils");
private static OpenStreetMapUtils instance = null;
private JSONParser jsonParser;
public OpenStreetMapUtils() {
jsonParser = new JSONParser();
}
public static OpenStreetMapUtils getInstance() {
if (instance == null) {
instance = new OpenStreetMapUtils();
}
return instance;
}
private String getRequest(String url) throws Exception {
final URL obj = new URL(url);
final HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() != 200) {
return null;
}
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
public Map<String, Double> getCoordinates(String address) {
Map<String, Double> res;
StringBuffer query;
String[] split = address.split(" ");
String queryResult = null;
query = new StringBuffer();
res = new HashMap<String, Double>();
query.append("http://nominatim.openstreetmap.org/search?q=");
if (split.length == 0) {
return null;
}
for (int i = 0; i < split.length; i++) {
query.append(split[i]);
if (i < (split.length - 1)) {
query.append("+");
}
}
query.append("&format=json&addressdetails=1");
log.debug("Query:" + query);
try {
queryResult = getRequest(query.toString());
} catch (Exception e) {
log.error("Error when trying to get data with the following query " + query);
}
if (queryResult == null) {
return null;
}
Object obj = JSONValue.parse(queryResult);
log.debug("obj=" + obj);
if (obj instanceof JSONArray) {
JSONArray array = (JSONArray) obj;
if (array.size() > 0) {
JSONObject jsonObject = (JSONObject) array.get(0);
String lon = (String) jsonObject.get("lon");
String lat = (String) jsonObject.get("lat");
log.debug("lon=" + lon);
log.debug("lat=" + lat);
res.put("lon", Double.parseDouble(lon));
res.put("lat", Double.parseDouble(lat));
}
}
return res;
}
}
How to call the above working class:
public class GetCoordinates {
static String address = "The White House, Washington DC";
public static void main(String[] args) {
Map<String, Double> coords;
coords = OpenStreetMapUtils.getInstance().getCoordinates(address);
System.out.println("latitude :" + coords.get("lat"));
System.out.println("longitude:" + coords.get("lon"));
}
}
use XPath is a easy way.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<xpath_expression>);
as for , try something like below:
/xxx/td[#nowrap='']/text()
Related
I am trying to filter some objects from a JSON response in Java. Below is my code. I need to get the Genre Object from the response and print it separately. Anyone who knows how that can be done?
I have did the RestAPI call from omdb. This is a just a simple project which I am trying to build. To basically analyze the type of genres that were released in particular years.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import sun.org.mozilla.javascript.internal.json.JsonParser;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class OmdbApiService {
//public static final String Search_Url = "http://www.omdbapi.com/?s=TITLE&apikey=APIKEY";
//public static final String Search_Url = "http://www.omdbapi.com/?t=TITLE&plot=PLOT&apikey=APIKEY";
public static final String Search_Plot = "http://www.omdbapi.com/?i=TITLE&plot=PLOT&apikey=APIKEY";
private static String sendGetRequest(String requestURL){
StringBuffer response = new StringBuffer();
try {
URL url = new URL(requestURL);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
InputStream stream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
String line;
while ((line = buffer.readLine()) != null) {
response.append(line);
}
buffer.close();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response.toString();
}
private static String searchMoviebyID(String title, String plot, String key) {
try {
title = URLEncoder.encode(title, "UTF-8"); // To omit the spaces in between the titles
plot = URLEncoder.encode(plot, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String requestUrl = Search_Plot
.replaceAll("TITLE", title)
.replaceAll("PLOT", plot)
.replaceAll("APIKEY", key);
return sendGetRequest(requestUrl);
}
/*private static String filterbyGenres(){
try {
}
}*/
public static void main(String[] args) {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836","full","6d****87");
System.out.println(jsonResponse);
/*Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = new JsonParser().parse(jsonResponse);
System.out.println(gson.toJson(jsonResponse));*/
}
}
Output:
{"Title":"The Dark Knight Rises","Year":"2012","Rated":"PG-13","Released":"20 Jul 2012","Runtime":"164 min","Genre":"Action, Thriller","Director":"Christopher Nolan","Writer":"Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)","Actors":"Christian Bale, Gary Oldman, Tom Hardy, Joseph Gordon-Levitt","Plot":"Despite his tarnished reputation after the events of The Dark Knight, in which he took the rap for Dent's crimes, Batman feels compelled to intervene to assist the city and its police force which is struggling to cope with Bane's plans to destroy the city.","Language":"English, Arabic","Country":"UK, USA","Awards":"Nominated for 1 BAFTA Film Award. Another 38 wins & 102 nominations.","Poster":"https://m.media-amazon.com/images/M/MV5BMTk4ODQzNDY3Ml5BMl5BanBnXkFtZTcwODA0NTM4Nw##._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"8.4/10"},{"Source":"Rotten Tomatoes","Value":"87%"},{"Source":"Metacritic","Value":"78/100"}],"Metascore":"78","imdbRating":"8.4","imdbVotes":"1,372,667","imdbID":"tt1345836","Type":"movie","DVD":"03 Dec 2012","BoxOffice":"$448,130,642","Production":"Warner Bros. Pictures","Website":"http://www.thedarkknightrises.com/","Response":"True"}
This is the output, can I know how do I filter out just the Genre in this output.
Extra help : If someone could tell me how to print the output in separate lines, it will be helpful.
You can parse it using the jackson library. Can you try this code?
Jackson:
// jackson library import
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
// ...
private static String filterByGenres(String jsonResponse) {
String genres = "";
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readValue(jsonResponse, JsonNode.class);
// Considering when there are no API results
if(jsonNode != null || jsonNode.get("Genre") != null) {
genres = jsonNode.get("Genre").asText();
}
} catch (Exception e) {
// handle to exception
}
return genres;
}
public static void main(String[] args) {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836", "full", "6d****87");
// The result of the API is the argument.(json format string)
String genres = filterByGenres(jsonResponse);
System.out.println(genres); // Action, Thriller
}
Gson:
public static void main(String[] args) {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836", "full", "6d****87");
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(jsonResponse).getAsJsonObject();
JsonElement genreObject = jsonObject.get("Genre");
System.out.println(genreObject.getAsString()); // Action, Thriller
}
Extra help:
Extra help : If someone could tell me how to print the output in separate lines, it will be helpful.
public void prettyPrint() {
String jsonResponse = OmdbApiService.searchMoviebyID("tt1345836", "full", "6d****87");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = new JsonParser().parse(jsonResponse);
String prettyJson = gson.toJson(jsonElement);
System.out.println(prettyJson);
}
So, I'm trying to extract JSON from the Guardian newspaper API.
Basically I can get everything except the author which is crucial.
How do or what is a different way of extracting this.
Many thanks in advance I'm new to all this and have asked questions i n the past to no avail any advice would be greatly appreciated.
QueryUtils.Java
package com.example.android.newsapp;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class QueryUtils {
private static final String TAG = QueryUtils.class.getSimpleName();
public static Context context;
private QueryUtils() {
}
public static List<News> fetchNews(String requestUrl) {
URL url = createUrl(requestUrl);
String json_response = null;
try {
json_response = makeHttpRequest(url);
Log.i(TAG, json_response);
} catch (IOException e) {
e.printStackTrace();
}
List<News> news = extractFromJson(json_response);
return news;
}
private static URL createUrl(String StringUrl) {
URL url = null;
try {
url = new URL(StringUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return url;
}
private static String makeHttpRequest(URL url) throws IOException {
String json_response = "";
if (url == null) {
return json_response;
}
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == 200) {
inputStream = httpURLConnection.getInputStream();
json_response = readFromString(inputStream);
} else {
Log.e(TAG, "Error" + httpURLConnection.getResponseCode());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return json_response;
}
private static String readFromString(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
} private static String extractString(JSONObject newInfo, String stringName) {
String str = null;
try {
str = newInfo.getString(stringName);
} catch (JSONException e) {
Log.e(TAG, context.getString(R.string.query_util_error_extract_string) + stringName);
}
if (str != null) {
return str;
} else {
return context.getString(R.string.empty_string);
}
}
private static List<News> extractFromJson(String news_json) {
if (TextUtils.isEmpty(news_json)) {
return null;
}
List<News> news = new ArrayList<News>();
try {
JSONObject baseJson = new JSONObject(news_json);
JSONArray news_array = baseJson.getJSONObject("response").getJSONArray("results");
for (int i = 0; i < news_array.length(); i++) {
JSONObject currentNews = news_array.getJSONObject(i);
String name = currentNews.getString("sectionName");
String title = currentNews.getString("webTitle");
String date = currentNews.getString("webPublicationDate");
String url = currentNews.getString("webUrl");
JSONArray tags = baseJson.getJSONArray("tags");
String contributor = null;
if (tags.length() == 1) {
JSONObject contributorTag = (JSONObject) tags.get(0);
contributor = extractString(contributorTag, context.getString(R.string.query_util_json_web_title));
} else {
//no contributor
contributor = context.getString(R.string.empty_string);
}
News mNews = new News(name, title, date, url, contributor);
news.add(mNews);
}
} catch (JSONException e) {
e.printStackTrace();
}
return news;
}
}
This is the JSON that I'm extracting from.
https://content.guardianapis.com/search?q=debate&tag=politics/politics&from-date=2014-01-01&api-key=test
This is the Data-Provider..
http://open-platform.theguardian.com/documentation/
I too had trouble getting the author. There were two changes I made that resolved the issue.
First, I did have to change the add the &show-tags=contributor to the url.
Second, I had to tweak your your if statement in parsing to read. Instead of :
contributor = extractString(contributorTag, context.getString(R.string.query_util_json_web_title));
I replaced with :
contributor = contributorTag.getString("webTitle");
(The key "webTitle" contains the author's name)
A problem for you is the url you used doesn't give the tag array which contains the webTitle key, even after I adjusted it with the &show-tags=contributor.
The url I used is:
http://content.guardianapis.com/search?&show-tags=contributor&q=%27tech%27&api-key=2bbbc59c-5b48-46a5-83d3-8435d3136348
The full QueryUtils.java file is:
package com.example.android.technewsapps1;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public final class QueryUtils {
private static final String LOG_TAG = QueryUtils.class.getSimpleName();
private QueryUtils() {
}
/**
* Query the Guardian dataset and return a list of NewsStory objects.
*/
public static List<NewsStory> fetchNewsStoryData(String requestUrl) {
// Create URL object
URL url = createUrl(requestUrl);
// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = null;
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
Log.e(LOG_TAG, "Problem making the HTTP request.", e);
}
// Extract relevant fields from the JSON response and create a list of NewsStories
List<NewsStory> newsStories = extractFeatureFromJson(jsonResponse);
// Return the list of NewsStories
return newsStories;
}
/**
* Returns new URL object from the given String URL.
*/
private static URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Problem building the URL ", e);
}
return url;
}
/**
* Make an HTTP request to the given URL and return a String as the response.
*/
private static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the newsStory JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// Closing the input stream could throw an IOException, which is why
// the makeHttpRequest(URL url) method signature specifies than an IOException
// could be thrown.
inputStream.close();
}
}
return jsonResponse;
}
/**
* Convert the {#link InputStream} into a String which contains the
* whole JSON response from the server.
*/
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
/**
* Return a list of NewsStory objects that has been built up from
* parsing the given JSON response.
*/
private static List<NewsStory> extractFeatureFromJson(String newsStoryJSON) {
// If the JSON string is empty or null, then return early.
if (TextUtils.isEmpty(newsStoryJSON)) {
return null;
}
// Create an empty ArrayList that we can start adding news stories to
List<NewsStory> newsStories = new ArrayList<>();
// Try to parse the JSON response string. If there's a problem with the way the JSON
// is formatted, a JSONException exception object will be thrown.
// Catch the exception so the app doesn't crash, and print the error message to the logs.
try {
// Create a JSONObject from the JSON response string
JSONObject baseJsonResponse = new JSONObject(newsStoryJSON);
//Create the JSONObject with the key "response"
JSONObject responseJSONObject = baseJsonResponse.getJSONObject("response");
//JSONObject responseJSONObject = baseJsonResponse.getJSONObject("response");
// Extract the JSONArray associated with the key called "results",
// which represents a list of news stories.
JSONArray newsStoryArray = responseJSONObject.getJSONArray("results");
// For each newsStory in the newsStoryArray, create an NewsStory object
for (int i = 0; i < newsStoryArray.length(); i++) {
// Get a single newsStory at position i within the list of news stories
JSONObject currentStory = newsStoryArray.getJSONObject(i);
// Extract the value for the key called "webTitle"
String title = currentStory.getString("webTitle");
// Extract the value for the key called "sectionName"
String sectionName = currentStory.getString("sectionName");
// Extract the value for the key called "webPublicationDate"
String date = currentStory.getString("webPublicationDate");
// Extract the value for the key called "url"
String url = currentStory.getString("webUrl");
//Extract the JSONArray with the key "tag"
JSONArray tagsArray = currentStory.getJSONArray("tags");
//Declare String variable to hold author name
String authorName = null;
if (tagsArray.length() == 1) {
JSONObject contributorTag = (JSONObject) tagsArray.get(0);
authorName = contributorTag.getString("webTitle");
}
// Create a new NewsStory object with the title, section name, date,
// and url from the JSON response.
NewsStory newsStory = new NewsStory(title, sectionName, date, url, authorName);
// Add the new NewsStory to the list of newsStories.
newsStories.add(newsStory);
}
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the newsStory JSON results", e);
}
// Return the list of earthquakes
return newsStories;
}
}
I making a java application that give the user notification with the weather conditions.
i used the yahoo weather API provided by yahoo like that link :
http://weather.yahooapis.com/forecastrss?w=2502265
and all i have to do is to change the eight numbered code that is in the URL in order to change the city.
that's working perfect, but there are two problems facing me now:
the first one, i want to implement a lot of weather forecast sources in my application not just the yahoo weather and i can't find a similar service in any other weather forecast websites.
the second one, i want to obtain the codes of all the cities in yahoo weather as for sure i won't ask the user to enter his city code, but to enter his city name and i'll match it with the code.
and here is the code that works with me in java:
the code to return the XML file:
package search;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public class Process {
public static void main(String[] args) throws IOException {
Display disp = new Display();
Document doc = generateXML("1940345");
disp.getConditions(doc);
}
public static Document generateXML(String code) throws IOException {
String url = null;
String XmlData = null;
// creating the URL
url = "http://weather.yahooapis.com/forecastrss?w=" + code;
URL xmlUrl = new URL(url);
InputStream in = xmlUrl.openStream();
// parsing the XmlUrl
Document doc = parse(in);
return doc;
}
public static Document parse(InputStream is) {
Document doc = null;
DocumentBuilderFactory domFactory;
DocumentBuilder builder;
try {
domFactory = DocumentBuilderFactory.newInstance();
domFactory.setValidating(false);
domFactory.setNamespaceAware(false);
builder = domFactory.newDocumentBuilder();
doc = builder.parse(is);
} catch (Exception ex) {
System.err.println("unable to load XML: " + ex);
}
return doc;
}
}
the code to display the temperature and humidity in that city :
package search;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Display {
static void getConditions(Document doc) {
String city = null;
String unit = null;
try {
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("rss");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
NodeList nl = eElement
.getElementsByTagName("yweather:location");
for (int tempr = 0; tempr < nl.getLength(); tempr++) {
Node n = nl.item(tempr);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) n;
city = e.getAttribute("city");
System.out.println("The City Is : " + city);
}
}
NodeList nl2 = eElement
.getElementsByTagName("yweather:units");
for (int tempr = 0; tempr < nl2.getLength(); tempr++) {
Node n2 = nl2.item(tempr);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element e2 = (Element) n2;
unit = e2.getAttribute("temperature");
}
}
NodeList nl3 = eElement
.getElementsByTagName("yweather:condition");
for (int tempr = 0; tempr < nl3.getLength(); tempr++) {
Node n3 = nl3.item(tempr);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element e3 = (Element) n3;
System.out.println("The Temperature In " + city
+ " Is : " + e3.getAttribute("temp") + " "
+ unit);
}
}
NodeList nl4 = eElement
.getElementsByTagName("yweather:atmosphere");
for (int tempr = 0; tempr < nl4.getLength(); tempr++) {
Node n4 = nl4.item(tempr);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element e4 = (Element) n4;
System.out.println("The Humidity In " + city
+ " Is : " + e4.getAttribute("humidity"));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
You can use Metwit weather api simply passing latitude and longitude.
If you can implement them client-side: 200 request/day (ip based throttling) no authentication required. Worldwide coverage, JSON and REST compliant. You can register for extra API calls for free and if you still need it to call them server side the basic plan is pretty cheap.
Full disclosure: I own this API.
Take a look on this discussion. It seems relevant:
https://stackoverflow.com/questions/4876800/is-there-an-international-weather-forecast-api-that-is-not-limited-for-non-comme
Additionally type "weather forecast api" in google. There are tons of references to APIs that support several weather services.
Here's a list of Weather APIs that are available via the Temboo Java SDK:
https://temboo.com/library/keyword/weather/
You can use YQL (yahoo query language) to find the WOEID by city name like
var lclqry = escape('select * from geo.places where text="OKLAHOMA CITY"')
var lclurl = "http://query.yahooapis.com/v1/public/yql?q=" + lclqry + "&format=json&callback=?";
I know this is an old question, but i found it and as Sai suggested i have written code in java that send YQL query and retrieve WOEID number. Than it uses it to get weather from yahoo-weather-java-api. It needs gson dependecy which you can get by adding dependency to maven. I hope this will help someone.
EDIT
If there is more than one WOEID number for given town name, than getWeather returns weather for town with first WOEID returned.
CODE
Weather.java:
import com.github.fedy2.weather.YahooWeatherService;
import com.github.fedy2.weather.data.Channel;
import com.github.fedy2.weather.data.unit.DegreeUnit;
import com.google.gson.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.bind.JAXBException;
/**
*
* #author robert
*/
public class Weather
{
public Channel getWeather(String townName) throws CantFindWeatherException
{
try
{
String baseUrl = "http://query.yahooapis.com/v1/public/yql?q=";
String query =
"select woeid from geo.places where text=\"" +
townName + "\"";
String fullUrlStr = baseUrl + URLEncoder.encode(query, "UTF-8") +
"&format=json";
URL fullUrl = new URL(fullUrlStr);
ResultObject resultObject = null;
ResultArray resultArray = null;
try (InputStream is = fullUrl.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr))
{
String result = "";
String read;
while ((read = br.readLine()) != null)
{
result += read;
}
Gson gson = new Gson();
try
{
resultObject = gson.fromJson(result, ResultObject.class);
}
catch (com.google.gson.JsonSyntaxException ex)
{
resultArray = gson.fromJson(result, ResultArray.class);
}
}
Integer woeid = null;
if (resultObject != null)
{
if (resultObject.query.results != null)
{
woeid = resultObject.query.results.place.woeid;
}
}
else if (resultArray != null)
{
woeid = resultArray.query.results.place[0].woeid;
}
if (woeid != null)
{
YahooWeatherService service = new YahooWeatherService();
Channel channel = service.getForecast(woeid.toString(),
DegreeUnit.CELSIUS);
return channel;
}
else
{
throw new CantFindWeatherException();
}
}
catch (IOException | JsonSyntaxException | JAXBException ex)
{
throw new CantFindWeatherException(ex);
}
}
private static class ResultObject
{
public QueryObject query;
}
private static class ResultArray
{
public QueryArray query;
}
private static class QueryObject
{
public int count;
public String created;
public String lang;
public ResultsObject results;
}
private static class QueryArray
{
public int count;
public String created;
public String lang;
public ResultsArray results;
}
private static class ResultsObject
{
public Place place;
}
private static class ResultsArray
{
public Place[] place;
}
private static class Place
{
public int woeid;
}
}
CantFindWeatherException.java:
/**
*
* #author robert
*/
public class CantFindWeatherException extends Exception
{
public CantFindWeatherException()
{
}
public CantFindWeatherException(String message)
{
super(message);
}
public CantFindWeatherException(String message, Throwable cause)
{
super(message, cause);
}
public CantFindWeatherException(Throwable cause)
{
super(cause);
}
}
As for the first question, I've build a website using forecast.io. It's pretty good. Good API and 1000 free calls/day. It uses latitute/longitude to find the weather of a place.
As for the second question, I would resolve what the user puts in with the Google Geocoding Api. So when they search for "New York", you check if you already have the relevant coordinates in your database, otherwise, you do an API call to Google Geocoding.
The code below is what I use to call the Google Maps API to get the geocoded address for a raw address input.
This works fine if I do it for a handful of addresses at one time.
But if I run it in a loop on multiple addresses, I occasionally and unpredicatbly get this error:
java.lang.String cannot be cast to net.sf.json.JSONObject
It happens on a different input string each time I run the loop - so it doesn't seem to be related to the particular input data.
I might have expected to encounter a rate limitation in the Google Maps API but this error doesn't seem related to that?
Or is it?
package com.blogspot.unserializableone;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
public class GCoder {
private static final String URL = "http://maps.google.com/maps/geo?output=json";
private static final String DEFAULT_KEY = "ABQIAAAAxPbpriJFATP1kV4Jfg7FrhTHx8S3jtCwO1hw0XE3N9WOac4cqRQ8_vMKB22No44yFau5GRY7TcCHZA";
public static GAddress geocode(String address, String key) throws Exception {
URL url = new URL(URL + "&q=" + URLEncoder.encode(address, "UTF-8") + "&key=" + key);
URLConnection conn = url.openConnection();
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
IOUtils.copy(conn.getInputStream(), output);
output.close();
GAddress gaddr = new GAddress();
JSONObject json = JSONObject.fromObject(output.toString());
JSONObject placemark = (JSONObject) query(json, "Placemark[0]");
final String commonId = "AddressDetails.Country.AdministrativeArea";
gaddr.setFullAddress(query(placemark, "address").toString());
gaddr.setZipCode(query(placemark,
commonId + ".SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber").toString());
gaddr.setAddress(query(placemark,
commonId + ".SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName")
.toString());
gaddr.setCity(query(placemark, commonId + ".SubAdministrativeArea.SubAdministrativeAreaName")
.toString());
gaddr.setState(query(placemark, commonId + ".AdministrativeAreaName").toString());
gaddr.setLat(Double.parseDouble(query(placemark, "Point.coordinates[1]").toString()));
gaddr.setLng(Double.parseDouble(query(placemark, "Point.coordinates[0]").toString()));
return gaddr;
}
public static GAddress geocode(String address) throws Exception {
return geocode(address, DEFAULT_KEY);
} /* allow query for json nested objects, ie. Placemark[0].address */
private static Object query(JSONObject jo, String query) {
try {
String[] keys = query.split("\\.");
Object r = queryHelper(jo, keys[0]);
for (int i = 1; i < keys.length; i++) {
r = queryHelper(JSONObject.fromObject(r), keys[i]);
}
return r;
} catch (JSONException e) {
return "";
}
} /* help in query array objects: Placemark[0] */
private static Object queryHelper(JSONObject jo, String query) {
int openIndex = query.indexOf('[');
int endIndex = query.indexOf(']');
if (openIndex > 0) {
String key = query.substring(0, openIndex);
int index = Integer.parseInt(query.substring(openIndex + 1, endIndex));
return jo.getJSONArray(key).get(index);
}
return jo.get(query);
}
public static void main(String[] args) throws Exception {
System.out.println(GCoder.geocode("28 275, Mercedes, Buenos Aires, Argentina"));
System.out.println(GCoder.geocode("94103"));
System.out
.println(GCoder.geocode("peña 2700 capital federal cdad. autonoma de buenos aires"));
System.out.println(GCoder
.geocode("2700 peña, capital federal, ciudad autonoma de buenos aires, argentina"));
}
}
It looks like your error might be happening at this line --
JSONObject placemark = (JSONObject) query(json, "Placemark[0]");
Looking at your query method, it also seems like you are swallowing the JSONException and returning an empty string which then cannot be cast to a JSONobject. So, its not an exception from the Google Maps API, but some exception in your code. I'd suggest printing stackTrace on that JSONException to investigate further.
I am using ajax google API to search a string against google. It is returning me all HTML files which have all tags including text.
If I want to get text only, what should I use?
My program is in Java.
Regards
Manjot
I did some googling and found this:
http://www.ajaxlines.com/ajax/stuff/article/using_google_is_ajax_search_api_with_java.php
Here's the example code snippet from there:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.json.JSONArray; // JSON library from http://www.json.org/java/
import org.json.JSONObject;
public class GoogleQuery {
// Put your website here
private final String HTTP_REFERER = "http://www.example.com/";
public GoogleQuery() {
makeQuery("questio verum");
makeQuery("info:http://frankmccown.blogspot.com/");
makeQuery("site:frankmccown.blogspot.com");
}
private void makeQuery(String query) {
System.out.println(" Querying for " + query);
try
{
// Convert spaces to +, etc. to make a valid URL
query = URLEncoder.encode(query, "UTF-8");
URL url = new URL("http://ajax.googleapis.com/ajax/services/search/web?start=0&rsz=large&v=1.0&q=" + query);
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", HTTP_REFERER);
// Get the JSON response
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
String response = builder.toString();
JSONObject json = new JSONObject(response);
System.out.println("Total results = " +
json.getJSONObject("responseData")
.getJSONObject("cursor")
.getString("estimatedResultCount"));
JSONArray ja = json.getJSONObject("responseData")
.getJSONArray("results");
System.out.println(" Results:");
for (int i = 0; i < ja.length(); i++) {
System.out.print((i+1) + ". ");
JSONObject j = ja.getJSONObject(i);
System.out.println(j.getString("titleNoFormatting"));
System.out.println(j.getString("url"));
}
}
catch (Exception e) {
System.err.println("Something went wrong...");
e.printStackTrace();
}
}
public static void main(String args[]) {
new GoogleQuery();
}
}
As a side note, you should be careful not to violate the Google TOS:
"You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt"
- http://www.google.com/accounts/TOS