Issue
This Content is from Stack Overflow. Question asked by Usman Adam
I am trying to get JSON object from OpenSubtitles API. I don’t know what is wrong with my code. I am using OkHttpClient and here is my code in below
CODE
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{n "file_id": file_idn}");
Request request = new Request.Builder()
.url("https://api.opensubtitles.com/api/v1/download")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Api-Key", "Api-Key")
.build();
try {
Response response = client.newCall(request).execute();
/// get date from your response
String responseBody = response.body().string();
///// convert it to json object
JSONObject jsonObject = new JSONObject(responseBody);
Log.d("DATA: ", String.valueOf(jsonObject)); // this is where i get the error
response.close();
} catch (IOException e) {
e.printStackTrace();
Log.d("IO EXCEPTION", "TIMEOUT");
} catch (Exception e) {
e.printStackTrace();
Log.d("OTHER EXCEPTION", "OTHER EXCEPTION");
}
}
}).start();
JSON OBJECT
{
"link": "https://www.opensubtitles.com/download/2432D790A54D32B5F9CD2C94D36F1435FFA26DD2FBA87FBE151FE9C22B42D678E77611BB3614B9AF277447046BA0A46B4C081948A7D3781E51749530DCA59B90CC96CF10183A679924644498FE151EE24BCE4540010DDAB6F5ABB90BAC974D7362535BC872564929792BBDB792FF5EFD9F254DBE5F5A14BC274CDD45280C18B322FDCC64D8D84C306D8E17A6CE8488967554B8E5ABC26E3EE1CBB548F9E0BF6D38532D9136DC7AF82E56F442AD8E30E9C2A717D6B3D39FD8EC53EE8D395E434BBB378495C28F1B67EDC5485384A7454E9326A7FCCB4CC869B356ACE8EF9F88EDA89E14DD8F865078E7C2101403079A8E1F48CB48A90732B24AACF7872AB823ABD2A98CB9A9DD107FB53E21DDDE92AFC445897BC66B061CB2137EFD5C1DCE32FEECFB350B99535A18/subfile/Day.Shift.2022.1080p.NF.WEB-DL.DDP5.1.Atmos.srt",
"file_name": "Day.Shift.2022.1080p.NF.WEB-DL.DDP5.1.Atmos.srt",
"requests": 6,
"remaining": 94,
"message": "Your quota will be renewed in 21 hours and 44 minutes (2022-09-19 23:16:46 UTC) ",
"reset_time": "21 hours and 44 minutes",
"reset_time_utc": "2022-09-19T23:16:46.000Z"
}
LOGCAT
W/System.err: org.json.JSONException: End of input at character 0 of
W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:460)
W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:101)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:165)
W/System.err: at org.json.JSONObject.<init>(JSONObject.java:182)
W/System.err: at com.nomedia.restapi.MainActivity$1.run(MainActivity.java:104)
W/System.err: at java.lang.Thread.run(Thread.java:923)
I tried adding my token to header but it still show the same error
Thanks for your input
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.