import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import org.json.JSONObject; public class Myclass { public static void main(String[] args) throws Exception { try { URL url = new URL("https://parseapi.back4app.com/classes/City?count=1&limit=10&keys=name,population"); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); urlConnection.setRequestProperty("X-Parse-Application-Id", "mxsebv4KoWIGkRntXwyzg6c6DhKWQuit8Ry9sHja"); // This is the fake app's application id urlConnection.setRequestProperty("X-Parse-Master-Key", "TpO0j3lG2PmEVMXlKYQACoOXKQrL3lwM0HwR9dbH"); // This is the fake app's readonly master key try { BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } JSONObject data = new JSONObject(stringBuilder.toString()); // Here you have the data that you need System.out.println(data.toString(2)); } finally { urlConnection.disconnect(); } } catch (Exception e) { System.out.println("Error: " + e.toString()); } } } --------------------------------------------- This is the code I have so far for part 2. Am I doing this correctly? public class city { private String name; private int population; public City (String cityName, int cityPop) { name = cityName; population = cityPop; } }
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONObject;
public class Myclass {
public static void main(String[] args) throws Exception {
try {
URL url = new URL("https://parseapi.back4app.com/classes/City?count=1&limit=10&keys=name,population");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty("X-Parse-Application-Id", "mxsebv4KoWIGkRntXwyzg6c6DhKWQuit8Ry9sHja"); // This is the fake app's application id
urlConnection.setRequestProperty("X-Parse-Master-Key", "TpO0j3lG2PmEVMXlKYQACoOXKQrL3lwM0HwR9dbH"); // This is the fake app's readonly master key
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
JSONObject data = new JSONObject(stringBuilder.toString()); // Here you have the data that you need
System.out.println(data.toString(2));
} finally {
urlConnection.disconnect();
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
}
---------------------------------------------
This is the code I have so far for part 2. Am I doing this correctly?
public class city
{
private String name;
private int population;
public City (String cityName, int cityPop)
{
name = cityName;
population = cityPop;
}
}
![Part 2. Access all city population data from this API. Write a Java program to find cities in top 50% -
55% in terms of population.
• Must create city as Class / object, which contains: String name, and int population.
• To implement comparable interface at city Class, so that two cities can use compareTo( )
method based on population.
It's recommended to use Java library container, such as ArrayList. in your program. Refer to
Chapter 3. Also read generic type at assigned reading section in Chapter 1.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff22ca2d4-c686-4cfd-b3ec-e8491d59db9f%2F6b6a92ce-d958-4182-9160-53d32dbcf4d1%2Fycrtmyq_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)