解析Android的凌空JSONArray响应

解析Android的凌空JSONArray响应

本文介绍了解析Android的凌空JSONArray响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要送一个JSONArray GET请求,排球,和它的返回指定的JSON数组。下面是我的要求:

I'm sending a JSONArray GET request with Volley, and it's returning the specified JSON array. Here's my Request:

JsonArrayRequest getRequest = new JsonArrayRequest(url,
                    new Response.Listener<JSONArray>()
                    {
                        @Override public void onResponse(JSONArray response) {

                            Log.d("Response", response.toString());
                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override public void onErrorResponse(VolleyError error) {
                            Log.d("Error.Response", error.toString());
                        }
                    }
                  );
                  VolleySingleton.getInstance(this).addToRequestQueue(getRequest); //Call to get dashboard feed
}

正如你所看到的,我目前只登出的响应。我想,虽然解析阵列,并将结果显示在列表视图。这方面的文档不是很大,而且我pretty的绿色Android开发方面。什么是正确的方式来解析来自排球JSON数组,并将结果显示在列表视图中? 我已经收集,我应该使用 parseNetworkResponse ,但不知道如何实现。

As you can see, I'm currently just logging out the response. I want to parse the Array though and display the results in a list view. The documentation for this isn't great, and I'm pretty green in terms of Android dev. What is the proper way to parse a JSON array from Volley and display the results in a list view? I've gathered that I should use parseNetworkResponse, but not sure how to implement.

推荐答案

我建议你坚持到GSON库JSON解析。以下是嵌入式JSON处理凌空请求怎么会看那么:

I'd recommend to stick to the GSON library for JSON parsing. Here's how a Volley request with embedded JSON processing could look then:

import java.io.UnsupportedEncodingException;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

/**
 * Volley GET request which parses JSON server response into Java object.
 */
public class GsonRequest<T> extends Request<T> {

    /** JSON parsing engine */
    protected final Gson gson;

    /** class of type of response */
    protected final Class<T> clazz;

    /** result listener */
    private final Listener<T> listener;

    public GsonRequest(String url, Class<T> clazz, Listener<T> listener,
            ErrorListener errorListener) {
        super(Method.GET, url, errorListener);

        this.clazz = clazz;
        this.listener = listener;
        this.gson = new Gson();
    }

    @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));
        }
    }
}

让我们想象一下,你有位于 http://example.com/api/persons/ 服务器方法,它返回一个人的JSON数组;人是如下:

Let's imagine you have a server method located at http://example.com/api/persons/ which returns a JSON array of Person; Person is as follows:

public class Person {
    String firstName;
    String lastName;
}

我们可以这样调用上述方法:

We can call the abovementioned method like this:

GsonRequest<Person[]> getPersons =
        new GsonRequest<Person[]>("http://example.com/api/persons/", Person[].class,

            new Listener<Person[]>() {
                @Override
                public void onResponse(Person[] response) {
                    List<Person> persons = Arrays.asList(response);
                    // TODO deal with persons
                }

            }, new ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO deal with error
                }
            });

VolleyQueue.get().add(getPersons);

和最后响应听者我们得到Person的一个阵列,它可以被转换到列表并送入ListView的适配器。

And finally in response listener we get an array of Person which can be converted to list and fed to ListView's adapter.

这篇关于解析Android的凌空JSONArray响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:54