使用volley-android获取POST的HTTP正文响应 [英] Get the HTTP body response of a POST with volley-android

查看:64
本文介绍了使用volley-android获取POST的HTTP正文响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取我的应用程序中的一些数据,并通过对Volley使用HTTP POST请求向用户显示. 我的问题是我不知道如何获得服务器主体对请求的响应.在下面的代码中,响应始终为"200",它成为HTTP状态代码.

I am trying to get in my app some data and show to the user by using an HTTP POST request with Volley. My issue is that I don't know how to get the server Body response to the request. In the code below the response is always "200" which turns to be the HTTP status code.

如何提取身体反应并以字符串形式进行管理?

How can I extract the body response and manage as a string?

 RequestQueue requestQueue = Volley.newRequestQueue(this);
    final String requestBody = Utility.toCorrectCase(req);
    //Decommentare solo per il debug
    //Toast.makeText(getBaseContext(),requestBody, Toast.LENGTH_SHORT).show();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("VOLLEY", response);
            Bitmap myBitmap = QRCode.from(response).bitmap();
            qrcode.setImageBitmap(myBitmap);
            ViewGroup.LayoutParams params = qrcode.getLayoutParams();
            params.width =  qrDisplayedSize;
            params.height = qrDisplayedSize;
            qrcode.setLayoutParams(params);
            loadingText.setText(getString(R.string.prod_id_desc).concat(response));
            qrcode.setVisibility(View.VISIBLE);
            backButton.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("VOLLEY", error.toString());
            new SweetAlertDialog(getApplicationContext(), SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("Errore comunicazione server")
                    .setContentText("Qualcosa non ha funzionato!\nDescrizione errore:".concat(error.toString()))
                    .show();
            finish();
        }
    }) {
        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=utf-8";
        }

        @Override
        public byte[] getBody() throws AuthFailureError {
            try {
                return requestBody == null ? null : requestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                return null;
            }
        }

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            String responseString;
            responseString = String.valueOf(response.statusCode);

            return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
        }
    };
    requestQueue.add(stringRequest);

推荐答案

经过几天的搜索,我发现我没有正确覆盖networkResponse.因此,对于同样遇到麻烦的每个人,我的问题的答案是:必须重写parseNetworkResponse才能以最轻松的方式获取响应正文.

After days of searches, I've found out that I have not overridden correctly the networkResponse. So for everyone with my same trouble, the answer of my question is: parseNetworkResponse must be overridden to get response body in an easisest way.

            @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {

            if(response.statusCode==400){
                new SweetAlertDialog(getApplicationContext(), SweetAlertDialog.ERROR_TYPE)
                        .setTitleText("Errore comunicazione server")
                        .setContentText("Qualcosa non ha funzionato!\nCodice errore:"+response.statusCode)
                        .show();
                finish();
            }
            return super.parseNetworkResponse(response);
        }

这篇关于使用volley-android获取POST的HTTP正文响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆