Android使用Volley将编码的图像发送到Base64 [英] Android Send image encoded to Base64 using Volley

查看:141
本文介绍了Android使用Volley将编码的图像发送到Base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让用户选择一个图像,然后该图像将被转换为Base64.我正在尝试将Base64字符串附加到我的url( Json Format )中,如下所示:

I am letting the user select a image and then the image will be converted to Base64. I am trying to append the Base64 String to my url(Json Format) like in the below

http://codemoirai.esy.es/register.php?UserDetails={"Sex":"Male","Username":"joes","Bitmap":"iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAIAAAA2NdDLAAAAA3NCSVQICAjb4U\/gAAAgAEl......

但是我遇到这样的错误:

But i am getting a error like this:

 BasicNetwork.performRequest: Unexpected response code 414 for http://codemoirai.esy.es/register.php?UserDetails={"Sex":"Male","Username":"joes","Bitmap":"iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAIAAAA2NdDLAAAAA3NCSVQICAjb4U\/gAA...........

我能知道是什么导致了此错误吗?如何使用Volley发送编码为Base64格式的图像文件?

Can i know what is causing this error? How can send a image file which is encoded to Base64 format using Volley?

谢谢

推荐答案

响应代码414 的请求URI太长(您的base64图像字符串太长,无法放入url).

Response code 414 is Request-URI Too Long (Your base64 image string is too long to put in url).

服务器拒绝处理请求,因为Request-URI 比服务器愿意解释的时间更长.这种罕见的情况 仅当客户端未正确转换POST时才可能发生 当客户端请求到具有长查询信息的GET请求时 已进入重定向的URI黑洞"(例如, 重定向的URI前缀(指向其后缀),或者 服务器正受到尝试利用安全性的客户端的攻击 使用固定长度缓冲区进行读取的某些服务器中存在漏洞 或操纵Request-URI.

The server is refusing to service the request because the Request-URI is longer than the server is willing to interpret. This rare condition is only likely to occur when a client has improperly converted a POST request to a GET request with long query information, when the client has descended into a URI "black hole" of redirection (e.g., a redirected URI prefix that points to a suffix of itself), or when the server is under attack by a client attempting to exploit security holes present in some servers using fixed-length buffers for reading or manipulating the Request-URI.

因此,您应该将http get更改为http post并在http正文中发送base64图像

So you should change from http get to http post and send base64 image in http body

您的服务器必须处理http发布数据.我不知道您使用哪种语言来实现服务器端.所以我只发布客户样本 样本

Your server must handle http post data. I dont know what language you use to implement your server side. So i only post client sample Sample

public void uploadAvatar(String username,String sex, String accessToken, String image, Response.Listener<JSONObject> success, Response.ErrorListener error) {
    String endpoint = "your server api url";
    ScoinJsonRequest request = new ScoinJsonRequest(Request.Method.POST, endpoint, getuploadAvatarParams(user, sex, image), success, error);
    request.setRetryPolicy(new DefaultRetryPolicy(MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    requestQueue.add(request);
}

private Map<String, String> getuploadAvatarParams(String username,String sex,String stringBase64) 
 {
    Map<String, String> params = new HashMap<String, String>();
     params.put("username", username);
     params.put("gender", sex);
     params.put("ibase64", stringBase64);

    return params;
}

然后,您可以使用uploadAvatar函数并输入所有必需的参数. 关于服务器端,您可以搜索读取http post data + your language.我为您提供了一个 c#示例

Then you can use uploadAvatar function and input all the required params. About server side you can search read http post data + your language. I give you a link to c# example

这篇关于Android使用Volley将编码的图像发送到Base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆