gpt4 book ai didi

java - MusixMatch API 和 GSON : Using track. snippet.get 而不是 track.lyrics.get

转载 作者:行者123 更新时间:2023-11-30 08:13:26 27 4
gpt4 key购买 nike

我正在研究 Java 类介绍的最终项目。该项目的一部分涉及从 MusixMatch using their API 获取歌词片段。我可以使用 track.lyrics.get 从 API 获取歌词,但无法使用 tracks.snippet.get 获取片段。

我从这里找到的 Java 包装器开始:https://github.com/sachin-handiekar/jMusixMatch并添加了我自己的类以获取基于 track.snippet.get API 方法的代码片段。

当我运行该程序时,出现此错误:

java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at 
line 1 column 102 path $.message.body

我的 getSnippet 方法和适用的类如下。它们基于原始包装器中的 getLyrics 方法和类。

public Snippet getSnippet(int trackID) throws MusixMatchException {
Snippet snippet = null;
SnippetGetMessage message = null;
Map<String, Object> params = new HashMap<String, Object>();

params.put(Constants.API_KEY, apiKey);
params.put(Constants.TRACK_ID, new String("" + trackID));

String response = null;

response = MusixMatchRequest.sendRequest(Helper.getURLString(
Methods.TRACK_SNIPPET_GET, params));

Gson gson = new Gson();

try {
message = gson.fromJson(response, SnippetGetMessage.class);
} catch (JsonParseException jpe) {
handleErrorResponse(response);
}

snippet = message.getContainer().getBody().getSnippet();

return snippet;
}

Snippet

package org.jmusixmatch.entity.snippet;

import com.google.gson.annotations.SerializedName;

/**
* Created by kyledhebert on 4/30/15.
* Objects of this clas represent a lyric snippet from the
* MusixMatch API.
*/
public class Snippet {

@SerializedName("snippet_language")
private int snippetLanguage;

@SerializedName("restricted")
private int restricted;

@SerializedName("instrumental")
private int instrumental;

@SerializedName("snippet_body")
private String snippetBody;

@SerializedName("script_tracking_url")
private String scriptTrackingURL;

@SerializedName("pixel_tracking_url")
private String pixelTrackingURL;

@SerializedName("html_tracking_url")
private String htmlTrackingURL;

@SerializedName("updated_time")
private String updatedTime;

public int getSnippetLanguage() {
return snippetLanguage;
}

public void setSnippetLanguage(int snippetLanguage) {
this.snippetLanguage = snippetLanguage;
}

public int getRestricted() {
return restricted;
}

public void setRestricted(int restricted) {
this.restricted = restricted;
}

public int getInstrumental() {
return instrumental;
}

public void setInstrumental(int instrumental) {
this.instrumental = instrumental;
}

public String getSnippetBody() {
return snippetBody;
}

public void setSnippetBody(String snippetBody) {
this.snippetBody = snippetBody;
}

public String getPixelTrackingURL() {
return pixelTrackingURL;
}

public void setPixelTrackingURL(String pixelTrackingURL) {
this.pixelTrackingURL = pixelTrackingURL;
}

public String getScriptTrackingURL() {
return scriptTrackingURL;
}

public void setScriptTrackingURL(String scriptTrackingURL) {
this.scriptTrackingURL = scriptTrackingURL;
}

public String getHtmlTrackingURL() {
return htmlTrackingURL;
}

public void setHtmlTrackingURL(String htmlTrackingURL) {
this.htmlTrackingURL = htmlTrackingURL;
}

public String getUpdatedTime() {
return updatedTime;
}

public void setUpdatedTime(String updatedTime) {
this.updatedTime = updatedTime;
}
}

SnippetGetBody 类:

package org.jmusixmatch.entity.snippet.get;

import com.google.gson.annotations.SerializedName;

import org.jmusixmatch.entity.snippet.Snippet;

public class SnippetGetBody {

@SerializedName("snippet")
private Snippet snippet;

public Snippet getSnippet() {
return snippet;
}

public void setSnippet(Snippet snippet) {
this.snippet = snippet;
}
}

SnippetGetContainer 类:

package org.jmusixmatch.entity.snippet.get;

import com.google.gson.annotations.SerializedName;

import org.jmusixmatch.entity.Header;

public class SnippetGetContainer {

@SerializedName("body")
private SnippetGetBody body;

@SerializedName("header")
private Header header;

public SnippetGetBody getBody() {
return body;
}

public void setBody(SnippetGetBody body) {
this.body = body;
}

public Header getHeader() {
return header;
}

public void setHeader(Header header) {
this.header = header;
}
}

SnippetGetMessage 类:

package org.jmusixmatch.entity.lyrics.get;

import com.google.gson.annotations.SerializedName;

public class SnippetGetMessage {
@SerializedName("message")
private SnippetGetContainer container;

public void setContainer(SnippetGetContainer container) {
this.container = container;
}

public SnippetGetContainer getContainer() {
return container;
}
}

最佳答案

我无法重现您的确切错误消息,但我确实发现了以下错误:snippet_language 是一个字符串,而不是 int。将类型(以及关联的 getter 和 setter)更改为:

@SerializedName("snippet_language")
private String snippetLanguage;

我使用了来自 here 的示例 Json 响应使这项工作成功。如果这两项更改不能解决您的问题,请edit您的问题与实际的 Json 响应导致您的程序无法运行。

关于java - MusixMatch API 和 GSON : Using track. snippet.get 而不是 track.lyrics.get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30017508/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com