【问题标题】:Post a JSONArray with volley and retrieve a String使用 volley 发布 JSONArray 并检索字符串
【发布时间】:2015-02-01 17:57:47
【问题描述】:

我是 android 开发和使用 volley 的新手,有几个问题。我正在尝试通过 Post 发送 JSONArray 并尝试返回一个字符串作为响应。

我已经通过 github 下载了 volley 文件,并在工具箱中获得了 JsonArrayRequest.java。该文件包含我的任务所需的大量信息,因此我决定将其与StringRequest.java 混合使用,然后得到以下信息:

名为 SendingJsonArray.java 的新类

package com.android.volley.toolbox;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.UnsupportedEncodingException;

/**
 * A request for retrieving a {@link JSONArray} response body at a given URL.
 */
public class SendingJsonArray extends JsonRequest<JSONArray> {

    /**
     * Creates a new request.
     * @param url URL to fetch the JSON from
     * @param listener Listener to receive the JSON response
     * @param errorListener Error listener, or null to ignore errors.
     */
    public SendingJsonArray(String url, JSONArray JsonArry, Listener<JSONArray> listener, ErrorListener errorListener) {
        super(Method.POST, url, JsonArry, listener, errorListener);
    }

    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString =
                new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONArray(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}
  1. 第一个奇怪的是SendingJsonArray()不包含int方法?它编译并且似乎没有它工作。

  2. 然后我将Method.GET 更改为Method.POST。它编译并似乎再次工作。

  3. 比我在SendingJsonArray 中添加"JSONArray JsonArry" 并得到以下错误:

    error: no suitable constructor found for JsonRequest(int,String,JSONArray,Listener<JSONArray>,ErrorListener)
    [javac]         super(Method.POST, url, JsonArry, listener, errorListener);
    [javac]         ^
    [javac]     constructor JsonRequest.JsonRequest(int,String,String,Listener<JSONArray>,ErrorListener) is not applicable
    [javac]       (actual argument JSONArray cannot be converted to String by method invocation conversion)
    [javac]     constructor JsonRequest.JsonRequest(String,String,Listener<JSONArray>,ErrorListener) is not applicable
    [javac]       (actual and formal argument lists differ in length)
    [javac] 1 error
    

好的,我知道问题出在哪里,但我不知道如何解决。原始构造函数在哪里,我该如何修改我的构造函数?

  1. 如果我得到这个工作,就会留下响应侦听器。我会尝试复制StringRequest.java 的侦听器并希望这会起作用?

    @Override
    protected void deliverResponse(String response) {
        mListener.onResponse(response);
    }
    
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }
    

为什么有两个,它们究竟是为了什么而创建的?抱歉,有关此的文档对我并没有真正的帮助:/

感谢您的帮助! :)

【问题讨论】:

标签: android arrays json post android-volley


【解决方案1】:

为了使用 volley 库发送 JSONArray 作为参数,我实现了这个名为 JSONPostArrayRequest 的类,它扩展了 JSONRequest,也许它可以帮助你:

public class JsonPostArrayRequest extends JsonRequest<JSONObject> {

    JSONArray params;
    public JsonPostArrayRequest(String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener,JSONArray params) {
        super(Method.POST, url, null, listener, errorListener);
        this.params=params;
    }

     @Override
     public byte[] getBody()  {
            if ( this.params != null && this.params.length() > 0) {
                return encodeParameters( this.params, getParamsEncoding());
            }
            return null;

      }

     private byte[] encodeParameters(JSONArray params, String paramsEncoding) {
            try {
                return params.toString().getBytes(paramsEncoding);
            } catch (UnsupportedEncodingException uee) {
                throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
            }
        }

        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {
                String jsonString =
                        new String(response.data, HttpHeaderParser.parseCharset(response.headers));
                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多