Android Volley POST JsonObject并获取JsonArray或JsonObject或其他响应

时间:2015-09-26 19:16:53

标签: android android-volley

在排球中,我们有一些从服务器检索数据的能力,如jsonObject,jsonArray和String。在下面的示例中,我们可以从服务器获取简单的jsonObject或jsonArray响应,

app.UseOpenIdConnectServer(options =>
{
    // Force the OpenID Connect server middleware to use JWT tokens
    // instead of the default opaque/encrypted token format used by default.
    options.AccessTokenHandler = new JwtSecurityTokenHandler();
});

public override Task HandleTokenRequest(HandleTokenRequestContext context)
{
    if (context.Request.IsPasswordGrantType())
    {
        var identity = new ClaimsIdentity(context.Options.AuthenticationScheme);
        identity.AddClaim(OpenIdConnectConstants.Claims.Subject, "unique identifier");

        var ticket = new AuthenticationTicket(
            new ClaimsPrincipal(identity),
            new AuthenticationProperties(),
            context.Options.AuthenticationScheme);

        // Call SetResources with the list of resource servers
        // the access token should be issued for.
        ticket.SetResources("resource_server_1");

        // Call SetScopes with the list of scopes you want to grant.
        ticket.SetScopes("profile", "offline_access");

        context.Validate(ticket);
    }

    return Task.FromResult(0);
}     

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    Audience = "resource_server_1",
    Authority = "http://localhost:61854"
});

我的问题是我想从这个方法发送jsonObject并从服务器获取jsonArray或jsonObject,并且我无法通过此方法从服务器获取简单的数组。例如,我必须使用此jsonObject过滤服务器响应:

public static void POST(HashMap<String, String> params, final Listeners.ServerResponseListener listener) {
    JsonObjectRequest req1 = new JsonObjectRequest(ApplicationController.URL, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("Response:", response.toString());
                    if (listener != null)
                        listener.onResultJsonObject(response);
                    else
                        Log.e(TAG,"Error: SetServerResponse interface not set");
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error: ", error.getMessage());
        }
    });
    ApplicationController.getInstance().addToRequestQueue(req1);
}

服务器返回jsonArray,我无法通过Volley响应获得

2 个答案:

答案 0 :(得分:3)

查看JsonArrayRequest的源代码。有一个构造函数接收JSONObject。你应该看看

答案 1 :(得分:1)

public class RetreiveData {

    public static final String TAG = RetreiveData.class.getSimpleName();

    public static void POST(String localhost, final HashMap<String, String> params, final Listeners.ServerResponseListener listener) {
        StringRequest post = new StringRequest(Request.Method.POST, localhost, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    if (listener != null)
                        listener.onResponse(response.toString());
                    else
                        Log.e(TAG, "Error: SetServerResponse interface not set");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("Error: ", e.getMessage());
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Error: ", error.toString());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = params;
                return map;
            }

            @Override
            public RetryPolicy getRetryPolicy() {
                setRetryPolicy(new DefaultRetryPolicy(
                        5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                return super.getRetryPolicy();
            }
        };
        ApplicationController.getInstance().addToRequestQueue(post);
    }
}