如果字符串的长度很长,Volley请求失败

时间:2019-04-03 20:37:46

标签: android android-volley multipart

我正在尝试使用截击做出多部分请求。当用户在编辑文本中输入的地址和标题的长度较小时,它可以完美工作;但是如果用户在编辑文本中输入更多文本,它将失败。

排球分段请求包含作为curl url的参数。我不明白真正的问题是什么。

final JSONObject jsonBody = new JSONObject();

        jsonBody.put("CategoryName", categories.get(categorySpinner.getSelectedItemPosition()).getCategoryName().replace(" ", "_"));
        jsonBody.put("SubCategoryName", subCategories.get(subCategorySpinner.getSelectedItemPosition()).replace(" ", "_"));
        jsonBody.put("City", "Rabwah");
        jsonBody.put("Address", etAddress.getText().toString());
        jsonBody.put("ProductTitle", etTitle.getText().toString());
        jsonBody.put("Description", etDescription.getText().toString());
        jsonBody.put("Price", Integer.parseInt(etPrice.getText().toString()));
        jsonBody.put("flgNegotiable", negotiableCheckBox.isChecked());
        jsonBody.put("flgUsed", usedCheckBox.isChecked());
        jsonBody.put("flgNew", newCheckBox.isChecked());
        jsonBody.put("ContactNumber", Integer.parseInt(etNumber.getText().toString()));
        jsonBody.put("UserID", preferences.getInt("id", 2));
        String url;
        if (adsModel != null) {
            jsonBody.put("ProductAddID", adsModel.getProductAddID());
            url = UrlConstants.updatepost + "?Json=" + jsonBody;

        } else {
            url = UrlConstants.submitAd + "?Json=" + jsonBody;
        }
        VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
            @Override
            public void onResponse(NetworkResponse response) {
                Log.d("onResponse: ", response.toString());
                try {
                    k.dismiss();
                } catch (Exception e) {
                }
                Toasty.success(context, "Ad Posted", Toast.LENGTH_SHORT).show();
                finish();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {



                try {
                    k.dismiss();
                } catch (Exception e) {
                }
                Toasty.error(context, "some error occured", Toast.LENGTH_SHORT).show();

                error.printStackTrace();

            }
        }) {


            @Override
            protected Map<String, VolleyMultipartRequest.DataPart> getByteData() throws AuthFailureError {
                Map<String, DataPart> params = new HashMap<>();



                    params.put("file1", new DataPart("file_avatar.jpg", getbyte(resultUri1), "image/jpeg"));




                return params;
            }


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                //params.put("Content-Type", "application/json; charset=UTF-8");
                params.put("Accept", "application/json; charset=UTF-8");
                return params;
            }
        };


        RequestQueue requestQueue = VolleySingleton.getInstance(context).getRequestQueue();

        requestQueue.add(multipartRequest);


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

请帮助我,我将非常感激

2 个答案:

答案 0 :(得分:0)

您似乎将数据附加到url,因此增加了其长度。一些服务器对URL的长度有限制,这可能是问题所在。由于您是在进行POST请求,所以为什么不将数据放入请求正文中,因为它可以处理大量数据。

答案 1 :(得分:0)

尝试这个

在build:gradle

implementation 'com.android.volley:volley:1.1.1'

在onCreate方法中全局声明

RequestQueue requestQueue;

在onCreate()中:

requestQueue = Volley.newRequestQueue(this);


String url = "www.yourwebsite.com/example.php";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            jsonParser(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(CreatePostQuery.this, "Error! Please try again later...", Toast.LENGTH_SHORT).show();
        }
    }) {
        protected Map<String, String> getParams() {
            Map<String, String> MyData = new HashMap<String, String>();
            MyData.put("userid", userid);
            MyData.put("posttext", posttext);
            MyData.put("email", email);
            return MyData;
        }
    };
    requestQueue.add(stringRequest);
}

public void jsonParser(String jsontext) {
    try {
        JSONObject emp = (new JSONObject(jsontext));
        String result = emp.getString("result");
        if (result.equals("successful")) {
            Toast.makeText(CreatePostQuery.this, "Post uploaded successfully", Toast.LENGTH_SHORT).show();
        }
        if (result.equals("error")) {
            Toast.makeText(CreatePostQuery.this, "Error! Please try again later...", Toast.LENGTH_SHORT).show();
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}