【问题标题】:Upload picture as Base64 string with volley returns warning: LogSlowRequest将图片上传为 Base64 字符串并带有 volley 返回警告:LogSlowRequests
【发布时间】:2017-01-23 17:36:18
【问题描述】:

我想将图片从我的应用上传到服务器。由于无法进行 ftp 连接,我想通过 POST 请求上传图片。

我在我的应用程序中使用 volley。当我将图片转换为 Base64 字符串并将其发送到我的服务器时,我收到警告:

D/Volley:[50697] BasicNetwork.logSlowRequests:HTTP 响应 请求=http://domain.com/api.php0x3ce314bb 正常 1> [lifetime=5264], [size=2], [rc=200], [retryCount=0]

这是我的上传类:

public class Upload {

    private Context context;

    public Upload(Context _context){
        context = _context;
    }

    public void upload(final File file){
        RequestQueue queue = Volley.newRequestQueue(context);
        StringRequest sr = new StringRequest(Request.Method.POST,"http://domain.com/api.php", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("DEBUG","RESPONSE: " + response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {}
        }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<>();
                params.put("a","text");
                params.put("b",convertPic(file.getPath()));
                params.put("c","text");
                return params;
            }
        };
        queue.add(sr);
    }

    private String convertPic(String path)
    {
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

    private String convertString(String s){
        return Base64.encodeToString((s).getBytes(),Base64.NO_WRAP);
    }
}

为了测试,我的 php 脚本只检查 $_POST 字段:

<?php
    header('content-type: text/plain; charset=utf-8');
    header("access-control-allow-origin: *");

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

    if(isset($post['a']) && $post['a'] != "" && isset($post['b']) && $post['b'] != "" && isset($post['c']) && $post['c'] != ""){
        echo 'OK';
    }

【问题讨论】:

    标签: java php android upload android-volley


    【解决方案1】:

    添加重试:

    sr.setRetryPolicy(new DefaultRetryPolicy(0,0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    

    使:DefaultRetryPolicy.DEFAULT_MAX_RETRIES 为 0

    【讨论】:

    • 上传工作,分别检查 $_POST 字段(参见 php 代码)。但我不知道为什么会收到此警告。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多