首页 > 解决方案 > 如何解决使用 volley 解析登录 json 的问题?

问题描述

    public void userLogin() {
        try {
            String URL = "http://influence360.in/orezmiapp/index.php/mobile/login";
            System.out.println("Arun URL :- " + URL);
            final JSONObject jsonBody = new JSONObject();
            jsonBody.put("type", "2");
            jsonBody.put("email", email);
            jsonBody.put("password", password);

            final String mRequestBody = jsonBody.toString();

            Log.e("JSON String is :- ", " " + mRequestBody);

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


                    try {
                        Toast.makeText(activity, "loginactivity entered", Toast.LENGTH_LONG).show();
                        JSONObject jsonObject = new JSONObject(response);
                        int status = jsonObject.getInt("status");
                        Toast.makeText(activity, status, Toast.LENGTH_LONG).show();
                        if (status == 1000) {
                            JSONObject data = jsonBody.getJSONObject("data");
                            //sessionManager.setUserid(data.getString("Cust_id"));
                            // sessionManager.setBirthday(data.getString("Dob"));
                            sessionManager.setUserfirstname(data.getString("Cust_Name"));
                            startActivity(new Intent(LoginScreen.this, Profile.class));
                            finish();
                        } else {
                            Toast.makeText(activity, "Hello", Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        Toast.makeText(activity, e.toString(), Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener()

            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("LOG_VOLLEY", error.toString());
                }
            })

            {
                @Override
                public String getBodyContentType() {
                    return "application/json; charset=utf-8";
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();

                    return headers;
                }

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

                        Log.e("Response String : -", " " + responseString);

                    }
                    return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(activity);
            requestQueue.add(stringRequest);
            stringRequest.setRetryPolicy(new

                    DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        }

我得到类型 200 org.json.Integer 的错误值不能转换为 JSONObject。请帮我解决这个问题。我没有得到任何解决方案。提前谢谢你。请帮助我。如果有人提供解决方案,我将非常感激。提前谢谢你。如果有人有解决方案,请给我。

标签: androidjson

解决方案


推荐阅读