MusixMatch API和GSON:使用track.snippet.get而不是track.lyrics.get

时间:2015-05-03 18:08:17

标签: java json gson deserialization

我正在研究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;
  }
}

1 个答案:

答案 0 :(得分:1)

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

@SerializedName("snippet_language")
private String snippetLanguage;

我使用here的示例Json响应来完成这项工作。如果这两项更改无法解决您的问题,请edit提出您的问题以及使您的计划失效的实际Json响应。