android- 使用 gson 和 retrofit 获取 json 值
android- Getting json values using gson and retrofit
我正在对 http 调用使用改造,对 json 解析使用 gson。我引用了这些链接:a-smart-way-to-use-retrofit and 。但是我得到 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;
}
}
}
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);
我建议使用:jsonschema2pojo 从 JSON 生成 pojo。这是合适的 SimpleResponseHandler
class :
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;
}
}
}
我正在对 http 调用使用改造,对 json 解析使用 gson。我引用了这些链接:a-smart-way-to-use-retrofit and 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;
}
}
}
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);
我建议使用:jsonschema2pojo 从 JSON 生成 pojo。这是合适的 SimpleResponseHandler
class :
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;
}
}
}