Hello, to try to create an application in java, I decided to make my first tests on the API Osu !. Only, I get this error when I turn my answer into a JSON object :
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:505)
at org.json.JSONObject.<init>(JSONObject.java:215)
at org.json.JSONObject.<init>(JSONObject.java:399)
at fr.Maxime3399.OsuAPITest.JavaRequest.request(JavaRequest.java:52)
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:9)
Exception in thread "main" java.lang.NullPointerException
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:21)
Here is my code :
And here is the answer in the console :
Thanks for your help
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:505)
at org.json.JSONObject.<init>(JSONObject.java:215)
at org.json.JSONObject.<init>(JSONObject.java:399)
at fr.Maxime3399.OsuAPITest.JavaRequest.request(JavaRequest.java:52)
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:9)
Exception in thread "main" java.lang.NullPointerException
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:21)
Here is my code :
package fr.Maxime3399.OsuAPITest;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.json.JSONObject;
public class JavaRequest {
private static HttpURLConnection con;
public static JSONObject request(String url, String params){
JSONObject result = null;
try {
byte[] postData = params.getBytes(StandardCharsets.UTF_8);
try {
URL myurl = new URL(url);
con = (HttpURLConnection) myurl.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Java client");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.write(postData);
}
StringBuilder content;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String line;
content = new StringBuilder();
while ((line = in.readLine()) != null) {
content.append(line);
}
}
System.out.println(content.toString());
result = new JSONObject(content.toString());
} finally {
con.disconnect();
}
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.json.JSONObject;
public class JavaRequest {
private static HttpURLConnection con;
public static JSONObject request(String url, String params){
JSONObject result = null;
try {
byte[] postData = params.getBytes(StandardCharsets.UTF_8);
try {
URL myurl = new URL(url);
con = (HttpURLConnection) myurl.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Java client");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.write(postData);
}
StringBuilder content;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String line;
content = new StringBuilder();
while ((line = in.readLine()) != null) {
content.append(line);
}
}
System.out.println(content.toString());
result = new JSONObject(content.toString());
} finally {
con.disconnect();
}
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
And here is the answer in the console :
[{"user_id":"12592290","username":"Maxime3399","join_date":"2018-07-04 13:37:49","count300":"459223","count100":"59541","count50":"9502","playcount":"3038","ranked_score":"960883730","total_score":"1945905042","pp_rank":"267669","level":"66.5828","pp_raw":"935.106","accuracy":"93.9571762084961","count_rank_ss":"46","count_rank_ssh":"2","count_rank_s":"358","count_rank_sh":"1","count_rank_a":"162","country":"FR","total_seconds_played":"260248","pp_country_rank":"12222","events":[]}]
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:505)
at org.json.JSONObject.<init>(JSONObject.java:215)
at org.json.JSONObject.<init>(JSONObject.java:399)
at fr.Maxime3399.OsuAPITest.JavaRequest.request(JavaRequest.java:52)
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:9)
Exception in thread "main" java.lang.NullPointerException
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:21)
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:505)
at org.json.JSONObject.<init>(JSONObject.java:215)
at org.json.JSONObject.<init>(JSONObject.java:399)
at fr.Maxime3399.OsuAPITest.JavaRequest.request(JavaRequest.java:52)
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:9)
Exception in thread "main" java.lang.NullPointerException
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:21)
Thanks for your help