Reputation: 505
I've read this post on how to do a POST request in Java. I don't understand how I can implement this in a JSON parser. This is what I tried so far:
public class JSONParser {
private String read(BufferedReader bufferedReader) throws IOException {
//Creates new StringBuilder to avoid escaping chars
StringBuilder stringBuilder = new StringBuilder();
//Gets the currentLine
String currentLine;
while((currentLine = bufferedReader.readLine()) !=null ){
//Adds the currentLine to the stringBuild if the currentLine is not null
stringBuilder.append(currentLine);
}
//Returns the StringBuilder is String format
return stringBuilder.toString();
}
public JSONObject readJsonFromUrl(String JSONurl) throws IOException, JSONException {
InputStream url = new URL(JSONurl).openStream();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url));
String jsonText = read(bufferedReader);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
url.close();
}
}
public void printJSON() throws IOException, JSONException {
JSONObject json = readJsonFromUrl("http://grwn.ddns.net:1337/locations");
System.out.print(json);
//for (Integer i = 0; i < json.getJSONArray("damage_or_theft_car").length(); i++) {
// System.out.println(json.getJSONArray("damage_or_theft_car")
//.getJSONObject(i).get("hood_id"));
//}
}
}
When I run this code with a link which doesn't require a POST request it all works fine but when I run this code on a link which DOES require a POST request I get the following error:
Exception in thread "main" java.io.FileNotFoundException: http://grwn.ddns.net:1337/locations
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1872)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.URL.openStream(URL.java:1045)
at com.company.JSONParser.readJsonFromUrl(JSONParser.java:30)
at com.company.JSONParser.printJSON(JSONParser.java:42)
at com.company.Main.main(Main.java:33)
Could someone help me out or point me in the right direction?
Upvotes: 0
Views: 288
Reputation: 948
I think you need to specify that you want to make a POST request after you open the connection, maybe try something like this.
public JSONObject readJsonFromUrl(String JSONurl) throws IOException, JSONException {
URL url = new URL(JSONurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProprtry("Content-type", "application/JSON");
try {
BufferedReader bufferedReader = new BufferedReader(conn. getInputStream());
String jsonText = read(bufferedReader);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
url.close();
}
}
Upvotes: 1
Reputation: 21
Possible causes for this issue might be one of the following; 1) Nothing to read/fetch from url 2) Check proxy settings, might be something configured on proxy for access to above url 3) Hostname mapping missing on your host
Some of the ways to verify this manually are as follows; 1) Try to access url from the web browser with proxy setting, see if you can get the desired raw json 2) Try to access url from the web browser without proxy setting, see if you can get the desired raw json 3) If step 1) or Step2) is successful, try same from your java code
Upvotes: 0