从JsonArrayRequest volley发送帖子到服务器不工作

时间:2016-12-24 03:10:20

标签: php android android-volley

我已经尝试了一些关于从JsonArrayRequest volley发送到服务器的帖子的例子,但它没有工作,日志中没有错误..

所以,当我在服务器上拨打帖子时,帖子无法获取并且我的数据在不通话时显示,但现在当发布电话时,数据未显示

这个代码

public void JSON_DATA_WEB_CALL() {
            CustomJsonArrayRequest request = new CustomJsonArrayRequest (GET_JSON_DATA_HTTP_URL,
                    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
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String, String> params = new HashMap<String, String>();
                    params.put("name", "value");
                    return params;
                }
            };
            requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(request);
    }

这是新类的代码

    public class CustomJsonArrayRequest extends JsonRequest<JSONArray> {

        public CustomJsonArrayRequest(String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
            super(Method.POST, url, null, listener, errorListener);
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("name", "value");
            return params;
        }

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

after webcall

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

        for(int i = 0; i<array.length(); i++) {

            GetDataAdapterRiwayat GetDataAdapter2 = new GetDataAdapterRiwayat();

            JSONObject json = null;
            try {

                json = array.getJSONObject(i);

                GetDataAdapter2.setIdJalanRiwayat(json.getString(JSON_ID_JALAN));
                GetDataAdapter2.setNamaJalanRiwayat(json.getString(JSON_NAMA_JALAN));
                GetDataAdapter2.setLatitudeRiwayat(json.getString(JSON_LATITUDE));
                GetDataAdapter2.setLongitudeRiwayat(json.getString(JSON_LONGITUDE));
                GetDataAdapter2.setFotoSebelumRiwayat(json.getString(JSON_FOTO_SEBELUM));
                GetDataAdapter2.setFotoSetelahRiwayat(json.getString(JSON_FOTO_SETELAH));
                GetDataAdapter2.setIdRiwayat(json.getString(JSON_ID_RIWAYAT));

            } catch (JSONException e) {

                e.printStackTrace();
            }
            GetDataAdapter1.add(GetDataAdapter2);
        }

        recyclerViewadapterRiwayat = new RecyclerViewAdapterRiwayat(GetDataAdapter1, this);

        recyclerView.setAdapter(recyclerViewadapterRiwayat);
    }

3 个答案:

答案 0 :(得分:1)

如果GET仍无效

,请使用POST

例如

CustomJsonArrayRequest request = new CustomJsonArrayRequest (GET_JSON_DATA_HTTP_URL + "?&name=value",
                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) {

                    }
                });

并更改课程CustomJsonArrayRequest

super(Method.POST, url, null, listener, errorListener);

super(Method.GET, url, null, listener, errorListener);

答案 1 :(得分:0)

试试这种方式

div.div
  p.paragraph
    | about 3 lines of text... (dynamic)
  button.button-i-want-to-center
    | click_me
  

编辑答案

___________________________
| Text.....................|
| Text.....................|
| Text.....................|
|          ______          |
|         |BUTTON|         | <--- Center of div: 
|                          |      I want the button to ignore 
|                          |      the text.
|                          |
|                          |
___________________________

答案 2 :(得分:0)

使用它:

public class BaseRequest extends Request<JSONArray> {
    private static final String TAG = BaseRequest.class.getSimpleName();
    private final Gson gson = new Gson();


    private final Response.Listener listener;

    public BaseRequest(String url, Response.Listener responseListener, Response.ErrorListener listener) {
        super(Request.Method.GET, url, listener);
        Log.e(TAG, "Requesting url : " + url);
        this.listener = responseListener;
    }
    public BaseRequest(int method, String url, Response.Listener responseListener, Response.ErrorListener listener) {
        super(method, url, listener);
        Log.e(TAG, "Requesting url : " + url);
        this.listener = responseListener;
    }

    @Override
    public Response<JSONArray> parseNetworkResponse(NetworkResponse response) {

        try {
            String json = null;
            json = new String(
                    response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            JSONArray result = new JSONArray(json);

           return Response.success(
                        result,
                        HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {

            return Response.error(new ParseError(e));

        } catch (JsonSyntaxException e) {

            return Response.error(new ParseError(e));
            e.pri

        } catch (JSONException e) {
            return Response.error(new ParseError(e));
        }


    }

    @Override
    protected VolleyError parseNetworkError(VolleyError volleyError) {
        return volleyError;
    }

    @Override
    protected void deliverResponse(JSONArray response) {
        listener.onResponse(response);
    }


}

并提出请求:

BaseRequest baseRequest = new BaseRequest(Request.Method.POST, url, new Response.Listener() {
            @Override
            public void onResponse(JSONArray response) {

                //Response as JsonArray
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Error : " + error.getMessage());

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();

               //Your parameters
                return params;
            }
        };

queue.add(baseRequest);