我在java中使用Unirest和/ resolve请求来获取用户提供的轨道的ID。我的代码过去工作得很好,然后大约1-2个月前API返回:http://hastebin.com/ofidoxuyec.xml,这解释了为什么当我尝试解析响应时会发生这种情况:
com.mashape.unirest.http.exceptions.UnirestException: java.lang.RuntimeException: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:143)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
at net.masterzach32.swagbot.music.player.SoundCloudAudio.<init>(SoundCloudAudio.java:46)
at net.masterzach32.swagbot.commands.music.PlayCommand.execute(PlayCommand.kt:91)
at net.masterzach32.swagbot.EventHandler.onMessageEvent(EventHandler.kt:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sx.blah.discord.api.events.EventDispatcher.lambda$null$12(EventDispatcher.java:343)
at java.util.concurrent.CopyOnWriteArrayList.forEach(CopyOnWriteArrayList.java:890)
at sx.blah.discord.api.events.EventDispatcher.lambda$null$13(EventDispatcher.java:341)
at java.util.concurrent.ConcurrentHashMap.forEach(ConcurrentHashMap.java:1597)
at sx.blah.discord.api.events.EventDispatcher.lambda$null$14(EventDispatcher.java:340)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.concurrent.ConcurrentHashMap$EntrySpliterator.forEachRemaining(ConcurrentHashMap.java:3606)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at sx.blah.discord.api.events.EventDispatcher.lambda$dispatch$18(EventDispatcher.java:339)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at com.mashape.unirest.http.HttpResponse.<init>(HttpResponse.java:106)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:139)
... 29 more
Caused by: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at com.mashape.unirest.http.JsonNode.<init>(JsonNode.java:51)
at com.mashape.unirest.http.HttpResponse.<init>(HttpResponse.java:95)
... 30 more
Caused by: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONArray.<init>(JSONArray.java:106)
at org.json.JSONArray.<init>(JSONArray.java:145)
at com.mashape.unirest.http.JsonNode.<init>(JsonNode.java:48)
... 31 more
它发生在我提供的任何链接上,甚至是API文档中的示例轨道。奇怪的是API响应代码是200,这意味着没有错误吗?
如果我只是输入API调用,那么API调用在任何浏览器中都可以正常工作。有任何想法吗?谢谢!
代码:
(title,author,id和streamUrl是正确声明的所有字段,我的SoundCloud客户端ID有效。我使用/ resolve文档中的示例链接仅用于测试)
try {
HttpResponse<JsonNode> response = Unirest.get("http://api.soundcloud.com/resolve?url=http://soundcloud.com/matas/hobnotropic&client_id=" + App.prefs.getSCClientId())
.header("accept", "application/json")
.asJson();
if(response.getStatus() != 200)
App.logger.warn("Soundcloud API returned error code " + response.getStatus() + " for the URL " + url);
JSONObject json = response.getBody().getObject();
title = json.getString("title");
author = json.getJSONObject("user").getString("username");
id = json.getInt("id") + "";
if(!json.getBoolean("streamable"))
throw new NotStreamableException(getSource(), url);
else
streamUrl = json.getString("stream_url");
} catch (UnirestException e) {
e.printStackTrace();
}