Android 使用 Volley 发送编码为 Base64 的图像

标签 android android-volley

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

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

但是我收到这样的错误:

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

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

谢谢

最佳答案

响应 code 414是 Request-URI Too Long(您的 base64 图像字符串太长,无法放入 url)。

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 body 中发送 base64 图片

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

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;
}

然后您可以使用上传头像功能并输入所有必需的参数。 关于服务器端,您可以搜索阅读 http post data + your language。我给你一个链接 c# example

关于Android 使用 Volley 发送编码为 Base64 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194517/

相关文章:

java - 在一项 Activity 中使用 volley 进行两次 API 调用......好的做法?

android - 响应代码 304 和 200 时的 Volley 异常错误

Android MediaRecorder .prepare 未处理的 IOEception

java - 如何解决这个需求,Android条件序列化名称

android - RecyclerView 中的 setLayoutManager NullPointErexception

java - 如何在 Android 中的 Google map 选项菜单中添加指南针

java - SharedPreferences 已更新,但会在应用程序重置时重置

Android : Volley 1. 0.18 如何禁用请求缓存?

android - Volley 使用 StringRequest 和 RequestFuture 进行阻塞同步调用

使用 Volley 的 Android REST 客户端