首页 > 解决方案 > 将新数据从 android volley 放到 SQL Server

问题描述

我想使用 android-volley 来编辑我的 SQL Server 数据,我尝试使用 request 方法 put 更新我的数据库,并将数据更改为 JSON,并使用 Visual Studio 捕获 JSON,最后更改 SQL Server 数据

安卓工作室:

StringRequest stringRequest = new StringRequest(Request.Method.PUT,
                    url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {     
                Log.d("success", response);
                Toast.makeText(SystemActivity.this, "succesful!, Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("error", error.toString());
            }
        });


        getparams();

        mQueue.add(stringRequest);
        //put-volley

    }

    //put-volley

    protected Map<String, String> getparams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("NotesID", corusername);  //NotesID
        params.put("Password", renewpass);

        return params;
    }
    //put-volley

视觉工作室:

public HttpResponseMessage Put(string userID, [FromBody] Account account)
{
    try
    {
        using (DemoEntities entities = new DemoEntities())
        {
            var entity = entities.Account.FirstOrDefault(s => s.NotesID == userID);

            if (entity == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "customers with userid = " + userID + "is not found");
            }
            else
            {
                entity.NotesID = account.NotesID;
                entity.Password = account.Password;

                entities.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, entity);
            }

        }
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
    }
}

标签: c#sql-serverandroid-volley

解决方案


如我所见,您在 StringRequest 之外调用 getParams():

getparams(); 
mQueue.add(stringRequest);

相反,您需要在 StringRequest 范围内覆盖它,例如:

StringRequest stringRequest = new StringRequest(Request.Method.PUT, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //Response
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //Error
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
                params.put("NotesID", corusername);  //NotesID
                params.put("Password", renewpass);

                return params;
        }
        @Override
        public HashMap<String, String> getHeaders() {
                HashMap headers = new HashMap();
                headers.put("Content-Type", "application/json");
                return headers;
        }
};

//Add request to volley request queue
VolleyRequestQueue.getInstance().addToRequestQueue(stringRequest);

HTTP 400 也可能是因为标头 Content-Type 设置错误。因此,您必须在 StringRequest 覆盖方法中设置标头,例如:

@Override
public HashMap<String, String> getHeaders() {
        HashMap headers = new HashMap();
        headers.put("Content-Type", "application/json");
        return headers;
}

此外,为了将 JSON 正确绑定到您的操作,您已修改操作以在参数中包含属性 [FromBody]。因此,您必须在请求中定义正确的标头:

contentType:"application/json"
//or
headers.put("Content-Type", "application/json");

还要先在 Postman 中测试您的 PUT 请求。


推荐阅读