【问题标题】:Getting Server Error while connecting to MS Sql from Android/Volley从 Android/Volley 连接到 MS Sql 时出现服务器错误
【发布时间】:2026-01-04 21:30:01
【问题描述】:

我正在为我的 Android 应用程序使用 Volley 从我的服务器获取数据。它在我的本地 Windows 机器上使用 XAMPP 5.6 完美运行。

我将它部署在 Windows 服务器上(带有 PHP 的 IIS)。当我使用邮递员检查请求时,请求运行正常。但是当我从我的 android 代码中检查它时,它返回 ServerError

我进一步检查,如果我从我的代码中注释掉 headers.put("Content-Type", "application/x-www-form-urlencoded"); 行,它适用于 IIS/PHP

从 android 应用程序运行它是否需要任何设置?如果我从邮递员那里运行它,它正在运行并给出预期的结果。

我的代码

private void getclient(final String clientCode) {
    try {
        String URL = Global.APIURL + Global.getclient;            

        Map<String, String> params = new HashMap<>();

        CustomRequest strRequest = new CustomRequest(Request.Method.POST, URL, params, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Global.ShowLog("Response getclient:" + response);
                Global.hideProgressDialog();
                try {
                    if (response != null && !TextUtils.isEmpty(response)) {
                        JSONObject jObj = new JSONObject(response);
                        if (jObj.getBoolean("error")) {
                            Global.ShowAlert(ThisPage.this, jObj.getString("message"));
                        } else {
                            JSONArray jsonArrayData=jObj.getJSONArray("data");
                            if(jsonArrayData.length()>0){

                                startActivity(new Intent(ThisPage.this, Login.class));
                                finish();
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Global.ShowLog("Response Login(Error):" + error.getMessage());                   
            }
        }) {

            /**
             * Passing some request headers
             * */
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/x-www-form-urlencoded");                    
                return headers;
            }

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("partycd", clientCode);

                return params;
            }
        };

    } catch (Exception e) {
        e.printStackTrace();
        Global.hideProgressDialog();
    }
}

【问题讨论】:

    标签: php android sql-server android-volley


    【解决方案1】:

    解决了。

    换行了:

    headers.put("Content-Type", "application/x-www-form-urlencoded");

    到:

    headers.put("Content-Type", "application/x-www-form-urlencoded; 字符集=UTF-8");

    【讨论】:

      最近更新 更多