如何使用齐射发布原始数据

时间:2019-07-02 06:30:32

标签: java android android-volley

我的API正常工作,但是当我使用android应用发送数据时,它没有插入数据。

btn.setOnClickListener(new View.OnClickListener() {
        @Override       
 public void onClick(View v) { JSONObject jsonBody = new JSONObject();
            try {
             `jsonBody.put("ID", "Android Volley Demo");
                jsonBody.put("Name", "BNK");
                final String requestBody = jsonBody.toString();
           StringRequest stringRequest=new StringRequest(Request.Method.POST, URL,
                        new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("VOLLEY", response);
                    }
                },
                        new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("VOLLEY", error.toString());
                    }
                }) {
                    @Override
                    public String getBodyContentType() {
                        return "application/json; charset=utf-8";
                    }
                    @Override
                    public byte[] getBody() throws AuthFailureError {
                        try {
                            return requestBody == null ? null : requestBody.getBytes("utf-8");
                        } catch (UnsupportedEncodingException uee) {
                            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                            return null;
                        }
                    }
                };
                requestQueue.add(stringRequest);

            } catch (JSONException e) {
                e.printStackTrace();
            }
         }


      });
   }
}

那是我的API。它正常工作。

public HttpResponseMessage Post([FromBody]Login log)
    {
            db.Logins.Add(log);
            db.SaveChanges();

            var message = Request.CreateResponse(HttpStatusCode.Created, log);

            message.Headers.Location = new Uri(Request.RequestUri + log.ID.ToString());

            return message;
    }

这是我的代码,我正在发布数据,但未插入SQL Server,但是当我通过发送原始数据来测试API时,其工作正常。

2 个答案:

答案 0 :(得分:0)

尝试一下:

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String ServerResponse) {
                                Log.i("VOLLEY Response : ", ServerResponse);
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Log.e("VOLLEY ErrorResponse : ", volleyError.toString());
                            }
                        }) {
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("Name", "BNK");
                        return params;
                    }

                };

            // Creating RequestQueue.
            RequestQueue requestQueue = Volley.newRequestQueue(context);

            // Adding the StringRequest object into requestQueue.
            requestQueue.add(stringRequest);

答案 1 :(得分:0)

选中这个

try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://...";
JSONObject jsonBody = new JSONObject();
jsonBody.put("firstkey", "firstvalue");
jsonBody.put("secondkey", "secondobject");
final String mRequestBody = jsonBody.toString();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.i("LOG_VOLLEY", response);
    }
}, 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 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);

        }
        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
    }
};

requestQueue.add(stringRequest);
} catch (JSONException e) {
 e.printStackTrace();
}