【问题标题】:android- Getting json values using gson and retrofitandroid- 使用 gson 和改造获取 json 值
【发布时间】:2015-07-11 16:34:54
【问题描述】:

我将改造用于 http 调用和 gson 用于 json 解析。我提到了这些链接:a-smart-way-to-use-retrofitdeserializing-inner-class-when-using-gson-and-retrofit。但我得到retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

这是我的 json 回复:

{
    "Document": [
        {
            "Name": "name",
            "Password": "passwrd"
        }
    ]
}

这是我的休息 api:

@GET("/ObjectTracking/login.php/")
public void getSimpleResponse(@Query("username") String username,
                              @Query("pwd") String password,
                              Callback<SimpleResponseHandler> handlerCallback);

这是我的适配器:

Gson gson = new GsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setEndpoint(Constants.BASE_URL)
            .setConverter(new GsonConverter(gson))
            .build();

MainActivity.class:

simpleRestApi.getSimpleResponse(email, password, new Callback<SimpleResponseHandler>() {
        @Override
        public void success(SimpleResponseHandler simpleResponseHandlers, Response response) {
            Log.e("RETROFIT SUCCESS", response.getBody().toString());
        }

        @Override
        public void failure(RetrofitError error) {
            Log.e("Retrofit error", error.getCause().toString());
        }
    });

SimpleResponseHandler.class:

public class SimpleResponseHandler {

    @Expose
    private List<Credentials> credentialList= new ArrayList<Credentials>();

    public List<Credentials> getCredentials() {
        return credentialList;
    }

    public void setCredentials(List<Credentials> credentialList) {
        this.credentialList = credentialList;
    }

    public class Credentials {

        @Expose
        private String mName;

        @Expose
        private String mPassword;

        public String getName() {
            return mName;
        }

        public void setName(String name) {
            this.mName = name;
        }

        public String getPassword() {
            return mPassword;
        }

        public void setPassword(String pwd) {
            this.mPassword = pwd;
        }
    }
}

【问题讨论】:

  • 这是一个解析错误。尝试对您的 Credential 变量使用正确的注释,例如 @SerializedName("Name") 和 @SerializedName("Password"),因为这是正确的命名。然后在你的 SimpleResponseHandler 类中给 List credentialList 另一个注解并给它@SerializedName("Document") 看看会发生什么。

标签: android json gson retrofit


【解决方案1】:

Java 对象字段名称应与 json 标记名称匹配或使用 @SerializedName()

public class Document {

        @Expose @SerializedName("Name")
        private String mName;

        @Expose @SerializedName("Password")
        private String mPassword;
}

Java 响应对象

public class SimpleResponse {

    @Expose @SerializedName("Document")
    private List<Document> credentialList= new ArrayList<Document>();

    public List<Document> getCredentials() {
        return credentialList;
    }

    public void setCredentials(List<Document> credentialList) {
        this.credentialList = credentialList;
    }
    }

更新的api方法

@GET("/ObjectTracking/login.php/")
public void getSimpleResponse(@Query("username") String username,
                              @Query("pwd") String password,
                              Callback<List<SimpleResponse>> handlerCallback);

【讨论】:

  • 好吧,但是如果另一个属性以某种方式出现怎么办?除了“文档”。我认为您在编辑帖子之前写的答案更好。
  • 我在没有 @Expose 注释的情况下尝试了这个,但仍然出现异常。无论如何,我会尝试您的回答并回复。谢谢。
  • @r7v 我试过你的答案,但仍然遇到同样的异常。
  • 我错过了文档的@SerializedName()。我已经更新了答案。立即尝试
  • 它不工作。我认为问题出在我作为响应得到的 json 上。
【解决方案2】:

我建议使用:jsonschema2pojo 从 JSON 生成 pojo。这是适当的SimpleResponseHandler 类:

public class SimpleResponseHandler {

@SerializedName("Document")
@Expose
private List<Document> document = new ArrayList<Document>();

/**
*
* @return
* The document
*/
public List<Document> getDocument() {
return document;
}

/**
*
* @param document
* The document
*/
public void setDocument(List<Document> document) {
this.document = document;
}

public class Document {

    @Expose
    private String Name;
    @Expose
    private String Password;

    /**
    *
    * @return
    * The Name
    */
    public String getName() {
    return Name;
    }

    /**
    *
    * @param Name
    * The Name
    */
    public void setName(String Name) {
    this.Name = Name;
    }

    /**
    *
    * @return
    * The Password
    */
    public String getPassword() {
    return Password;
    }

    /**
    *
    * @param Password
    * The Password
    */
    public void setPassword(String Password) {
    this.Password = Password;
    }

}

}

【讨论】:

  • @Sree14 你确定你得到了正确的 json 响应吗?
  • 我收到的回复与我在上面发布的相同
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 2014-05-29
  • 2020-05-23
  • 2017-07-29
  • 2016-05-09
  • 2014-10-16
  • 1970-01-01
相关资源
最近更新 更多