【发布时间】: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));
}
}
}
第一个奇怪的是
SendingJsonArray()不包含int方法?它编译并且似乎没有它工作。然后我将
Method.GET更改为Method.POST。它编译并似乎再次工作。-
比我在
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
好的,我知道问题出在哪里,但我不知道如何解决。原始构造函数在哪里,我该如何修改我的构造函数?
-
如果我得到这个工作,就会留下响应侦听器。我会尝试复制
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)); }
为什么有两个,它们究竟是为了什么而创建的?抱歉,有关此的文档对我并没有真正的帮助:/
感谢您的帮助! :)
【问题讨论】:
-
这可能有助于 GET:stackoverflow.com/questions/29761634/…,这可能有助于 POST:stackoverflow.com/questions/29779447/…
标签: android arrays json post android-volley