首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从android将新数据放入Server

从android将新数据放入Server
EN

Stack Overflow用户
提问于 2019-06-25 12:49:39
回答 1查看 546关注 0票数 0

我想使用android-volley编辑我的Server数据,我尝试使用请求方法put更新我的db,并将数据更改为JSON,并使用visual studio捕获JSON,最后更改Server数据。

安卓工作室:

代码语言:javascript
复制
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

视频工作室:

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-06-25 14:42:21

正如我所看到的,您正在StringRequest之外调用StringRequest():

代码语言:javascript
复制
getparams(); 
mQueue.add(stringRequest);

相反,您需要在StringRequest作用域中覆盖它,例如:

代码语言:javascript
复制
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可能是因为标头内容类型设置错误。因此,您必须在StringRequest overriden方法中设置标题,如:

代码语言:javascript
复制
@Override
public HashMap<String, String> getHeaders() {
        HashMap headers = new HashMap();
        headers.put("Content-Type", "application/json");
        return headers;
}

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

代码语言:javascript
复制
contentType:"application/json"
//or
headers.put("Content-Type", "application/json");

也要先在邮递员中测试你的投递请求。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56754485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档