【问题标题】:requestQueue volley doenst work (sending multiple requests)requestQueue volley 不起作用(发送多个请求)
【发布时间】:2018-09-18 11:18:27
【问题描述】:

我想上传多张图片(使用 base64 编码)。 我使用 for 发送这些图像:

for(int i =1; i<6; i++){
        bmp = ((BitmapDrawable)imgs[i].getDrawable()).getBitmap();
        String image = getEncoded64ImageStringFromBitmap(bmp);
        SendImage(image);
}

但它只发送 5 个请求中的 1 个或 2 个请求!这里也没有发生错误。我有一个在 onCreate 方法中初始化的 requestQueue。 这是我的截击请求:

private void SendImage( final String image) {
    String URL = APPURL;

    final StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonObject = new JSONObject(response);

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

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

    }
}) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {

        Map<String, String> params = new Hashtable<String, String>();

        params.put("image", image);
        return params;
    }
};
{
    requestQueue.add(stringRequest);
    Toast.makeText(AddProduct.this,"added "+requestQueue.getSequenceNumber(),Toast.LENGTH_SHORT).show();

}}

【问题讨论】:

  • 你是怎么知道的?你见过Toast吗?
  • 如果你的意思是错误,是的。我在这里删除了它。
  • 没有。我的意思是你是怎么知道的,前两个请求已经添加,而不是其余的?
  • 你的问题帮助我检​​查了更多并找到了答案。

标签: android api web-services android-studio android-volley


【解决方案1】:

你必须通过递归方法来做到这一点

喜欢

只需要打一次电话

multiFileUpload(uploadedFileCount);

然后它将处理递归模型中的所有文件。

private int totalFileCount = 6;
private int uploadedFileCount = 1;
private String URL = APPURL;
private multiFileUpload(int _uploadedFileCount)
{
final StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if((uploadedFileCount<6)
                        {
                           uploadedFileCount++;
                           multiFileUpload(uploadedFileCount);
                        }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
        new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    }
}) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {

        Bitmap bmp = ((BitmapDrawable)imgs[_uploadedFileCount].getDrawable()).getBitmap();
        String image = getEncoded64ImageStringFromBitmap(bmp);
        Map<String, String> params = new Hashtable<String, String>();
        params.put("image", image);
        return params;
    }
};
{
    requestQueue.add(stringRequest);
    Toast.makeText(AddProduct.this,"added "+requestQueue.getSequenceNumber(),Toast.LENGTH_SHORT).show();
}

}

【讨论】:

    【解决方案2】:

    我的问题出在后端。图片是同时出现的,我使用 time() 函数来命名它们,所以只有一两个文件保存在服务器中。

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 1970-01-01
      • 2013-11-19
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多