Android Volley onResponse获取数据

声芝士

在一个字符串登录请求登录中。在onResponse中,我返回的所有状态码都是200。我也想获取返回的数据。我不确定从现在开始该往哪里走?关于如何获取返回的数据的任何想法,而不仅仅是状态码?

    public void requestWithSomeHttpHeaders() {

    try {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String URL = "http://......";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("username", "yourusername");
        jsonBody.put("password", "yourpassword");
        final String mRequestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("VOLLEY", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

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

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String responseString = "";
                if (response != null) {
                    responseString = String.valueOf(response.statusCode);
                    // can get more details such as response.headers
                    System.out.println(responseString);
                }
                return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
            }
        };

        requestQueue.add(stringRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
板球运动员

优选地,定义一种方法。

private void doLogin() {
    // TODO: startActivity?
}

然后打电话给

 StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("VOLLEY", response);

            doLogin();
        }

如果要从服务器获取数据,那String response就是。检查您的Logcat。

正如在评论中提到的,使用StringRequest不执行getBodyparseNetworkResponse以及getBodyContentType可以更好地工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android Volley onResponse 不执行:(

Android volley requestString return null onResponse

Android:不调用Volley String请求onresponse

Android Wear Volley onResponse 错误 null

Android Volley无法获取数据作为响应

Android Volley 为什么JsonObjectRequest 不调用onResponse

Gson/Volley Android 从响应中获取消息而不是数据

使用 Android Volley 发布和获取 Json 数据

使用Volley的Android App获取比赛条件

通过Android-Volley获取JSON对象

使用Android Volley获取Instagram访问令牌

在Android中使用Volley提取数据

Android Volley访问缓存响应数据

Android Kotlin Volley 如何发送 JSON 数据

Volley onResponse 内的 MPAndroidChart

Volley onResponse方法未调用

无法使用android-volley从JSON数组获取数据到自定义listview

通过发送一些数据来获取 Jsonarray 作为 android volley 的响应

在 Volley 库中获取 Android 上的数据数量是否有任何限制?

无法在Android中使用Volley将数据从API获取到ArrayList

如何从服务器获取单个数据到android volley中的textview中?

Android Studio-使用Volley从服务器获取JSON数据

我如何使用Android Volley获取响应Cookie

Android Volley POST JsonObject并获取JsonArray或JsonObject或其他响应

Android Volley-获取响应标题并阅读html

使用Volley plus或OkHttp从服务获取Android更新通知进度

使用Volley进行Android Studio JSON解析:获取NullPointerException

Android Kotlin Volley 如何从 JSONArray 中获取值

在 Android 应用中使用 Volley 库解析对象数据