无法使用JSONObjectRequest将数据发布到服务器

时间:2017-10-27 05:07:02

标签: java android

以下是我将数据发布到服务器但无法发布的方法。 我必须将数据发布到服务器,我正在使用JsonObjectRequest是否有任何其他方法发布数据,请帮助我。

 private void postNoticeData() {
    final String noticeTitle = postTitle.getText().toString();
    final String description = postDescription.getText().toString();

    Map<String, String> params = new HashMap<String, String>();
    params.put(KEY_NOTICE_TITLE, noticeTitle);
    params.put(KEY_NOTICE_DESCRIPTION, description);



    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
            URL_DATA,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i("@school", "Response - " + response);
                    Toast.makeText(AddNoticeAdmin.this.getApplication(), "Added Successfully", Toast.LENGTH_SHORT).show();
                    AddNoticeAdmin.this.NavigateToNotice();
                    progressDialog.dismiss();

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("@school", "Error - " + error.getMessage());
            Toast.makeText(AddNoticeAdmin.this.getApplication(), "Check your Internet Connection", Toast.LENGTH_SHORT).show();
            progressDialog.dismiss();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonObjectRequest);
}

显示排球错误请帮帮我

1 个答案:

答案 0 :(得分:1)

在URl之后编写Post方法如下:

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
        URL_DATA, postNotice(),
        new Response.Listener<JSONObject>() {

   }

postNotice方法

private String postNotice() {
    // Your POJO Class();
    // the Data to be Posted using the PojoClass Object;

    return new Gson().toJson(pojoClassObject);
}

希望如此有帮助