android - volley JsonArrayRequest加入StringRequest帖子

时间:2016-12-21 14:11:55

标签: java android

我不想从JsonArrayRequest发送帖子到服务器,我找到了一些答案,我尝试了..

但是我收到了像like this

这样的错误

这是我的代码

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self resizeHeaderToFitSubview];
}

- (void)resizeHeaderToFitSubview
{
    UIView *header = self.tableView.tableHeaderView;
    [header setNeedsLayout];
    [header layoutIfNeeded];
    CGFloat height = CGRectGetHeight(header.subviews.firstObject.bounds);
    header.bounds = CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), height);
    self.tableView.tableHeaderView = nil;
    self.tableView.tableHeaderView = header;
}

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用此自定义类。

public class CustomJsonArrayRequest extends JsonRequest<JSONArray> {
/**
 * Creates a new request.
 * @param method the HTTP method to use
 * @param url URL to fetch the JSON from
 * @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
 *   indicates no parameters will be posted along with request.
 * @param listener Listener to receive the JSON response
 * @param errorListener Error listener, or null to ignore errors.
 */
public CustomJsonArrayRequest(int method, String url, JSONObject jsonRequest,
                              Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
            errorListener);
}

@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
        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));
    }
}
}

在您的情况下实施应该是这样的:

HashMap<String, String> params = new HashMap<String, String>();

public void JSON_DATA_WEB_CALL() {
    CustomJsonArrayRequest request = new CustomJsonArrayRequest (Request.Method.POST, GET_JSON_DATA_HTTP_URL, new JSONObject(params),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("CUSTOM_HEADER", "Yahoo");
            headers.put("ANOTHER_CUSTOM_HEADER", "Google");
            return headers;
        }
    };
    requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(request);
}