Android凌空通过图片上传发送两次信息

tom*_*ung 9 php android android-volley

我试图从我的发布数据发送到我的服务器的图像从android.为了实现这一点,我将64位编码的我的图像编码为字符串并使用android volley库发送它.但这会引起问题.由于某种原因,它有时会发送两次帖子,我无法弄清楚原因.下面是调用发送post请求的函数.我在那里放了一个破记标记,String url = "http://domain.com/ajax_ws.php";然后在protected Map<String, String> getParams() {我发现的String url = ...内容中只有一个被调用一次但是当它发送两个时,它protected Map...被调用两次.我在android排球上找不到任何文档,所以我不知道为什么会这样.调整位图大小,使图像字符串始终保持在100k到200k字符之间.我想也许这是一个大小问题,但我的服务器正在接收图像并解码它们,一切都很好.

 public void Sharing() {

    pd = ProgressDialog.show(getParent(), null, "Please Wait...");
    final String caption = mEtMessage.getText().toString();
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://domain.com/ajax_ws.php";
    StringRequest postRequest = new StringRequest(
            Request.Method.POST,
            url,
            new MyStringListener(),
            new MyErrorListener()
    ) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("token", "secretToken");
            params.put("mode", "createVoucher");
            params.put("user_id", ActivityLogin.id);
            params.put("deal_id", ActivitySharing.id_deal);
            params.put("user_id_company", ActivityRestaurantDetails.res.getId());
            params.put("user_img", pathImage);
            params.put("caption", caption);
            params.put("company_id", ActivityRestaurantDetails.res.getId());
            return params;

        }
    };
    queue.add(postRequest);
}
Run Code Online (Sandbox Code Playgroud)

知道为什么会这样吗?

Aru*_*ole 8

我能够通过两种方式解决这个问题.

首先是Snicolas建议的.换了RetryPolicy.只需将超时值设置为默认超时的两倍即可.工作得很好.您还可以尝试其他值.

request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Run Code Online (Sandbox Code Playgroud)

另一种方法是connection.setChunkedStreamingMode(0);openConnection方法HurlStack类中设置.

我正在创造RequestQueue这样的requestQueue = Volley.newRequestQueue(context, new HurlStack());

希望能帮助到你 :)


小智 6

解决方案是编辑重试策略,这也在此解释:(http://www.techstricks.com/avoid-multiple-requests-when-using-volley/).

但是,如果覆盖对你不起作用,那么你可能想重温一下你的凌空缓存逻辑.由于凌空缓存中的软ttl,结果从缓存中传递,同时它将另一个网络请求排队,该请求也将返回结果.因此,单个请求但有两个不同的结果.


Sni*_*las 4

Volley 使用 RetryPolicy 来处理请求,默认情况下使用指数退避算法发送请求最多 3 次。是否可能某些请求失败并重试?您是否收到请求的第一次调用的任何错误/成功日志?