Skip to content

Commit

Permalink
Fixed the null exception on country detector
Browse files Browse the repository at this point in the history
  • Loading branch information
platico committed May 27, 2016
1 parent 7180eea commit fa045ca
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
65 changes: 40 additions & 25 deletions src/analytics/LocationDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,40 @@ public class LocationDetector {
*/
public static String getCountryOf(String city){

String url = String.format("http://maps.googleapis.com/maps/api/geocode/json?address="+city+"&sensor=false"+"?key="+API_KEY);
HttpGet httpGet = new HttpGet(url);
String country = "";

try {
HttpEntity entity = defaultHttpClient.execute(httpGet).getEntity();
String retSrc = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(retSrc); //Convert String to JSON Object
country = parseJSONforCountry(jsonObject); //passes json to be parsed
if(country.equals("")){
System.out.println("Could not find country for: "+city);
}
else{
System.out.println("City: "+city);
System.out.println("Country: "+country);
if(!city.equals("")){

String url = String.format("http://maps.googleapis.com/maps/api/geocode/json?address="+city+"&sensor=false"+"?key="+API_KEY);
System.out.println(url);
HttpGet httpGet = new HttpGet(url);
String country = "";

try {
HttpEntity entity = defaultHttpClient.execute(httpGet).getEntity();
String retSrc = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(retSrc); //Convert String to JSON Object
country = parseJSONforCountry(jsonObject); //passes json to be parsed
if(country.equals("")){
System.out.println("Could not find country for: "+city);
}
else{
System.out.println("City: "+city);
System.out.println("Country: "+country);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();

return country;


}
else{
System.out.println("Can not search for empty city string...");
return "";
}

return country;


}
Expand All @@ -67,17 +78,21 @@ private static String parseJSONforCountry(JSONObject jsonObject){

JSONArray resultsArray = jsonObject.getJSONArray("results");
if (resultsArray.length() > 0) { //if it returned results for the specific city
JSONObject firstResult = resultsArray.getJSONObject(0); //gets the first results
JSONObject firstResult = resultsArray.getJSONObject(0); //gets the first result
JSONArray addressComponentsArray = firstResult.getJSONArray("address_components");
String country = "";
for (int i = 0; i < addressComponentsArray.length(); i++) {
JSONObject countryObject = addressComponentsArray.getJSONObject(i); //gets the object
JSONArray typeArray = countryObject.getJSONArray("types");
if (typeArray.get(0).equals("country")) { //if this JSONObject is a country object
country = countryObject.getString("long_name");
break; //breaks out of the loop
try{
if (typeArray.get(0).equals("country")) { //if this JSONObject is a country object
country = countryObject.getString("long_name");
break; //breaks out of the loop
}
}
catch (JSONException e){ //if the type does not exist it will throw exception
System.out.println(e.getCause());
}

}
if (country.equals("")) {
return ""; //if no country found returns empty string
Expand Down
20 changes: 10 additions & 10 deletions src/combiner/Combiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ public static void main(String[] args) throws JSONException, IOException {

if(args != null) {
// Collect Youtube comments and insert them to DB
YoutubeExporter.main(args);
Preprocessor.preprocessComments(args[4].split("=")[1], fc);


// Collect tweets and insert them to DB
// YoutubeExporter.main(args);
// Preprocessor.preprocessComments(args[4].split("=")[1], fc);
//
//
// // Collect tweets and insert them to DB
// TwitterExporter.main(args);
// Preprocessor.preprocessTweets(args[4].split("=")[1], fc);
//
// fc.exportFrequencies(); //creates frequencies.txt - sorted alphabetically
// fc.exportFrequenciesByValue(); //creates frequenciesByValue.txt - sorted by frequencies (descending order)
//
// if(args.length >= 5) {
// analyticsExtractor = new AnalyticsExtractor(args[4].split("=")[1]);
// //analyticsExtractor.analyze();
// }

if(args.length >= 5) {
analyticsExtractor = new AnalyticsExtractor(args[4].split("=")[1]);
analyticsExtractor.analyze();
}
}

}
Expand Down

0 comments on commit fa045ca

Please sign in to comment.