首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MusixMatch API和GSON:使用track.snippet.get而不是track.lyrics.get

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

Stack Overflow用户
提问于 2015-05-03 18:08:17
回答 1查看 582关注 0票数 1

我正在为Java类介绍的最后一个项目工作。该项目的一部分内容是从MusixMatch使用他们的API获得一段歌词片段。我可以使用track.lyrics.get从API中获得歌词,但不能使用tracks.snippet.get获得片段。

我从这里找到的一个Java开始:https://github.com/sachin-handiekar/jMusixMatch,并添加了我自己的类来获得一个基于Java方法的代码片段。

当我运行程序时,我会得到以下错误:

代码语言:javascript
复制
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at 
line 1 column 102 path $.message.body

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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;
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-04 15:37:05

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

代码语言:javascript
复制
@SerializedName("snippet_language")
private String snippetLanguage;

我使用了来自这里的示例Json响应来完成这项工作。如果这两个更改不能解决您的问题,请编辑您的问题与实际的Json响应,使您的程序不工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30017508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档