【问题标题】:500 exception while connecting to live server using Volley in android在 android 中使用 Volley 连接到实时服务器时出现 500 异常
【发布时间】:2017-01-30 12:34:58
【问题描述】:

我正在尝试使用 volley 身份验证连接到实时服务器并通过 POST 方法发送值。连接到本地服务器时运行良好,但连接到实时服务器时出现以下异常。

BasicNetwork.performRequest:“URL”的意外响应代码 500

服务器端错误如下

解析媒体类型“application/json”时出错;字符集=utf-8,应用程序/x-www-form-urlencoded; charset=UTF-8' 预期分隔符 ';'而不是 ',' 这是进入服务器端的错误

这是我的代码

public void userLogin(){
    showDialog(DIALOG_LOADING);

    final String json = new Gson().toJson(arr);

    StringRequest jsonObjReq = new StringRequest(Request.Method.POST,
            Const.URL_LOGIN,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    System.out.println("LOGIN Response :" + response);

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            NetworkResponse networkResponse = error.networkResponse;
            System.out.println("NetworkResponse "+ networkResponse);
            if (networkResponse!= null && networkResponse.statusCode == 401) {
                Login.this.runOnUiThread(new Runnable() {
                    public void run() {
                        invalidemail.setVisibility(View.GONE);
                        inactiveaccount.setVisibility(View.VISIBLE);
                    }
                });

            }
            error.printStackTrace();
            removeDialog(DIALOG_LOADING);
            toast_tv.setText("There is no Internet connection. Try again...!");
            toast.show();
        }
    }){

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

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

            String credentials = String.format("%s:%s",Const.auth_username,Const.auth_password);
            String auth = "Basic "
                    + Base64.encodeToString(credentials.getBytes(),
                    Base64.NO_WRAP);
            headers.put("Authorization", auth);
            headers.put("Content-Type", "application/json; charset=utf-8");

            return headers ;
        }
    };

    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(20 * DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 0, 0));
    Volley.newRequestQueue(this).add(jsonObjReq);
}

在服务器端,Jersey 正在与 Java 一起使用。

【问题讨论】:

  • 它可能是服务器端的错误。
  • 500 是内部服务器错误的错误状态。
  • 但它在 ios 中工作,并且同一服务器用于 android 和 ios。 @SripadRaj
  • 或者您可能必须向服务器发送错误的参数,使其崩溃。
  • 当我在发布前打印值时,它是正确的@SripadRaj

标签: android web-services rest android-volley


【解决方案1】:

最后我解决了这个问题。实际上代码没有问题,但我使用的 volley 版本已被弃用。当更新 volley 库时,它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多