简体   繁体   English

Android Volley请求Gson

[英]Android Volley Request Gson

Looking to use the class on the android developers website that uses Volley and Gson to fetch and deserialize json data from a url to parse into an array. 希望在android开发人员网站上使用该类,该类使用Volley和Gson从url获取和反序列化json数据以解析为数组。 I'm still a beginner so I was trying to instantiate the class but I'm not sure how to format the parameters. 我仍然是一个初学者,所以我试图实例化该类,但不确定如何格式化参数。 Tried looking for implementation but they seem to use other API's in addition. 试图寻找实现,但他们似乎还使用其他API。 The returned json only has one field and value so class object clazz has one field. 返回的json仅具有一个字段和值,因此类对象clazz具有一个字段。

public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Listener<T> listener;

/**
 * Make a GET request and return a parsed object from JSON.
 *
 * @param url URL of the request to make
 * @param clazz Relevant class object, for Gson's reflection
 * @param headers Map of request headers
 */
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
        Listener<T> listener, ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.clazz = clazz;
    this.headers = headers;
    this.listener = listener;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    return headers != null ? headers : super.getHeaders();
}

@Override
protected void deliverResponse(T response) {
    listener.onResponse(response);
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        String json = new String(
                response.data,
                HttpHeaderParser.parseCharset(response.headers));
        return Response.success(
                gson.fromJson(json, clazz),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    }
}
}

Just looking to get the example up and running So it at least can receive the data to display on screen. 只是希望使示例启动并运行,因此它至少可以接收要在屏幕上显示的数据。

First, you need to define a model class based on your expected JSON response. 首先,您需要根据预期的JSON响应定义模型类。

For example, If your JSON is Array of Users, then model class will look like this, 例如,如果您的JSON是用户数组,那么模型类将如下所示,

Users.Java 用户Java

public class Users extends Object implements Serializable {
    private static final long serialVersionUID = 1L;
    @SerializedName("users")
    private ArrayList<User> arrListUser;

    public ArrayList<User> getArrListBusinessObj() {
        return arrListUser;
    }

    public static class User extends Object implements Serializable  
    {
        private static final long serialVersionUID = 1L;

        private String user_id;
        private String name;
        private String artwork;

        public String getBusinessObjId() {
            return user_id;
        }

        public String getName() {
            return name;
        }

        public String getArtwork() {
            return artwork;
        }

    }
}

Then where you are receiving your network response, call below method : 然后在收到网络响应的地方,调用以下方法:

Users users = (Users)gson.fromJson(data, Users.class); //data is string form response from server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM