使得使用凌空GSON请求 [英] making a GSON request using volley
问题描述
我有以下的JSON响应
{
标签: [
{
listing_count:5,
目录:
{
源代码:源1,
数据: {
形象:此搜索,
名:NAME1
},
名:NAME1
}
]
},
{
listing_count:5,
目录:
{
源代码:源2,
数据: {
形象:imag2
名:NAME2
},
名:NAME2
}
]
}
]
}
我已经创建了下面的类GSON请求。如何让我的GSON请求和存储的值用凌空请求的响应。 应该采取什么GSON的要求是怎样的?
公共类标记列表{
ArrayList的<标签>标签;
公共静态类标签{
INT listing_count;
ArrayList的<信息>房源;
公众诠释getListing_count(){
返回listing_count;
}
公共无效setListing_count(INT listing_count){
this.listing_count = listing_count;
}
公众的ArrayList<信息> getListings(){
返回目录;
}
公共无效setListings(ArrayList中<清单及GT;上市){
this.listings =清单;
}
}
公共静态类房源{
字符串源;
数据数据;
字符串名称;
公共字符串的getSource(){
返回源;
}
公共无效的SetSource(字符串源){
this.source =来源;
}
公开数据的getData(){
返回的数据;
}
公共无效使用setData(数据数据){
this.data =数据;
}
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
}
公共静态类数据{
字符串图像;
字符串名称;
公共字符串的getImage(){
返回形象;
}
公共无效setImage(字符串图像){
this.image =图像;
}
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
}
只需创建一个GsonRequest类如下(摘自<一个href="https://developer.android.com/training/volley/request-custom.html">https://developer.android.com/training/volley/request-custom.html)
公共类GsonRequest&LT; T&GT;扩展请求&LT; T&GT; {
私人最终GSON GSON =新GSON();
私人最终类别&LT; T&GT; clazz所;
私人最终地图&LT;字符串,字符串&GT;头;
私人最终监听器&LT; T&GT;侦听器;
/ **
*做一个GET请求,并从JSON返回解析的对象。
*
*请求@参数的URL网址,使
*参数clazz所相关类对象,对于GSON的反思
*参数标题地图请求头
* /
公共GsonRequest(字符串URL,类&LT; T&GT; clazz中,地图&LT;字符串,字符串&GT;头,
收听LT; T&GT;监听器,ErrorListener errorListener){
超(Method.GET,网址,errorListener);
this.clazz = clazz所;
this.headers =头;
this.listener =侦听器;
}
@覆盖
公共地图&LT;字符串,字符串&GT; getHeaders()抛出AuthFailureError {
返回头!= NULL?标题:super.getHeaders();
}
@覆盖
保护无效deliverResponse(T响应){
listener.onResponse(响应);
}
@覆盖
受保护的响应&LT; T&GT; parseNetworkResponse(NetworkResponse响应){
尝试 {
JSON字符串=新的String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
返回Response.success(
gson.fromJson(JSON,clazz所),
HttpHeaderParser.parseCacheHeaders(响应));
}赶上(UnsupportedEncodingException E){
返回Response.error(新ParseError(e)条);
}赶上(JsonSyntaxException E){
返回Response.error(新ParseError(e)条);
}
}
}
现在在你的类文件(活动)只是调用这个类,如下所示:
请求队列排队= MyVolley.getRequestQueue();
GsonRequest&LT; MyClass的&GT; myReq =新GsonRequest&LT; MyClass的&GT;(Method.GET,
的http:// JSONURL /,
TagList.class,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
我们还需要创建牵引方法 -
- createMyReqSuccessListener() - 接收来自GsonRequest响应
- createMyReqErrorListener() - 来处理任何错误
如下:
私人Response.Listener&LT; MyClass的&GT; createMyReqSuccessListener(){
返回新Response.Listener&LT; MyClass的&GT;(){
@覆盖
公共无效onResponse(MyClass的响应){
//做你想要做的反应;
//就像response.tags.getListing_count();等等等等。
}
};
}
和
私人Response.ErrorListener createMyReqErrorListener(){
返回新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
//做你想做的事与error.getMessage();
}
};
}
我希望它会做出一些感觉。
I have the following json response
{
"tag": [
{
"listing_count": 5,
"listings": [
{
"source": "source1",
"data": {
"image": "image1",
"name": "name1"
},
"name": "name1"
}
]
},
{
"listing_count": 5,
"listings": [
{
"source": "source2",
"data": {
"image": "imag2",
"name": "name2"
},
"name": "name2"
}
]
}
]
}
I have created the following classes for GSON request. How do I make the GSON request and store the values for the response using a volley request. What should the GSON request be like?
public class TagList {
ArrayList<Tag> tags;
public static class Tag {
int listing_count;
ArrayList<Listings> listings;
public int getListing_count() {
return listing_count;
}
public void setListing_count(int listing_count) {
this.listing_count = listing_count;
}
public ArrayList<Listings> getListings() {
return listings;
}
public void setListings(ArrayList<Listings> listings) {
this.listings = listings;
}
}
public static class Listings {
String source;
Data data;
String name;
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class Data {
String image;
String name;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Just create a GsonRequest Class as follows (taken from https://developer.android.com/training/volley/request-custom.html)
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));
}
}
}
Now in your class file (Activity) just call the this class as follows:
RequestQueue queue = MyVolley.getRequestQueue();
GsonRequest<MyClass> myReq = new GsonRequest<MyClass>(Method.GET,
"http://JSONURL/",
TagList.class,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
We also need to create tow methods -
- createMyReqSuccessListener() - receive the response from GsonRequest
- createMyReqErrorListener() - to handle any error
as follows:
private Response.Listener<MyClass> createMyReqSuccessListener() {
return new Response.Listener<MyClass>() {
@Override
public void onResponse(MyClass response) {
// Do whatever you want to do with response;
// Like response.tags.getListing_count(); etc. etc.
}
};
}
and
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Do whatever you want to do with error.getMessage();
}
};
}
I hope it will make some sense.
这篇关于使得使用凌空GSON请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!