forum

Osu API Help

posted
Total Posts
4
Topic Starter
Maxime3399
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 :

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;

}

}


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)


Thanks for your help :)
bastoo0
The issue you have here is that the content you try to give to your JSONObject is an array. You should have {"user_id":"12592290","username":"Maxime3399"...} but you have [{"user_id":"12592290","username":"Maxime3399"...}] instead (which means that you have those [], that represent an array).

I suggest you to use a JSONArray instead of a JSONObject.
Topic Starter
Maxime3399
I tried to do this :
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.JSONArray;

public class JavaRequest {

private static HttpURLConnection con;

public static JSONArray request(String url, String params){

JSONArray 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);
}
}

result = new JSONArray(content.toString());

System.out.println(result.getString(1));

} finally {

con.disconnect();
}

}catch (Exception e) {

e.printStackTrace();

}
return result;

}

}

But I get this error message :
org.json.JSONException: JSONArray[1] not found.
at org.json.JSONArray.get(JSONArray.java:219)
at org.json.JSONArray.getString(JSONArray.java:461)
at fr.Maxime3399.OsuAPITest.JavaRequest.request(JavaRequest.java:53)
at fr.Maxime3399.OsuAPITest.MainClass.main(MainClass.java:9)


It's as if the list was empty, and when I tried to print (result.toString ()); I did not get anything.
bastoo0
If you have a single element in the array, its index is 0, not 1.
Replace the print line with
System.out.println(result.getString(0))

The first element of an array is 0 and the last one is its length -1. (Just so you can understand how to use the arrays).

If you don't have anything in your array when you print the entire array with result.toString(), please print the "content" variable and show me the result.
Please sign in to reply.

New reply