在Android上使用volley进行图像上传太慢

时间:2018-03-22 22:51:45

标签: android android-volley

我正在使用凌空将图像发送到服务器。问题是发送图像需要花费很多时间。 有什么想法吗?

private class UploadImage {
    Bitmap image;
    String name;
    String encodedImage = "";

UploadImage(Bitmap image, String name) {
    this.image = image;
    this.name = name;

    if(image != null) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
    }

    Response.Listener<String> responseListener = new Response.Listener<String>(){

        @Override
        public void onResponse(String response){
            try {
                JSONObject jsonResponse = new JSONObject(response);
                boolean success = jsonResponse.getBoolean("success");
                if(success){
                    String done = getResources().getString(R.string.done);
                    Toast.makeText(getApplicationContext(), done, Toast.LENGTH_SHORT).show();
                }
                else{
                    AlertDialog.Builder builder = new AlertDialog.Builder(Upload.this);
                    String error = getResources().getString(R.string.error);
                    String retry = getResources().getString(R.string.retry);
                    builder.setMessage(error)
                            .setNegativeButton(retry,null)
                            .create()
                            .show();
                }
            } catch (JSONException e){
                e.printStackTrace();
            }
        }

    };

和此:

    UploadRequest uploadRequest = new UploadRequest(encodedImage, UserId, name, token, responseListener);
    RequestQueue queue = Volley.newRequestQueue(Upload.this);

    DefaultRetryPolicy  retryPolicy = new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    uploadRequest.setRetryPolicy(retryPolicy); //send once.

    queue.add(uploadRequest);

这是不正常的,它总是需要上传图像10次。

0 个答案:

没有答案