php - Volley BasicNetwork.performRequest : Unexpected response code 413

标签 php android android-volley

我正在做一个用户注册是一个小模块的项目。我在这里使用了一些参数,例如姓名、电子邮件、联系方式、年龄、地址和个人资料图片。

//Here is my Android part Code
Where Profile_pic is the compressed byte[] of an image

这里是我的byte[]压缩方式

public static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    return output;
}

Android Volley Reuest 将用户数据存储到服务器

public void addUser(final String name, final String email, final String mobile, final String password, final String dob,
                          final String gender, final String address, final String profile_pic, final String latLng,
                          final String city, final String state, final String country, final String postalcode) {
    StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL_REQUEST_SMS, new Response.Listener<String>() {

        public String addresse;

        @Override
        public void onResponse(String response) {
            System.out.println("1");

            try {
                JSONObject responseObj = new JSONObject(response);


                boolean error = responseObj.getBoolean("error");
                String message = responseObj.getString("message");
                if (!error) {
                   ......
                } else {

                    Toast.makeText(context,
                            "Error: " + message,
                            Toast.LENGTH_LONG).show();
                }


            } catch (JSONException e) {


                Toast.makeText(context,
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(context,
                    error.getMessage() , Toast.LENGTH_SHORT).show();
        }
    }) {

        /**
         * Passing user parameters to our server
         * @return
         */
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("mobile", mobile);
            params.put("password", password);
            params.put("dob", dob);
            params.put("gender", gender);
            params.put("address", address);
            params.put("profile_pic", profile_pic);
            params.put("latLng", latLng);
            params.put("color", String.valueOf(color));
            params.put("user_level", "patient");
            params.put("city", city);
            params.put("state", state);
            params.put("country", country);
            params.put("postalcode", postalcode);
            return checkParams(params);
        }

        private Map<String, String> checkParams(Map<String, String> map) {
            for (Map.Entry<String, String> pairs : map.entrySet()) {
                if (pairs.getValue() == null) {
                    map.put(pairs.getKey(), "");
                }
            }
            return map;
        }
    };

    strReq.setShouldCache(false);
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq);
}

这是我上传图片的PHP代码

  $file_upload_url = __DIR__."/Upload/". $target_path;
// Get file name posted from Android App
$filename = 'user_'.$name.'_Profilepic'.'.png';
//check if the directory exists
if(!file_exists($file_upload_url)){
    if(!mkdir($file_upload_url,0755,true)){
             $error = error_get_last();
                echo $error['message'];
                     echo 'failed';
          }
    }
// Decode Image
$binary=base64_decode($profile_pic);
header('Content-Type: image/png');

$file = fopen($file_upload_url.$filename, 'wb');
// Create File
fwrite($file, $binary);
fclose($file);

这也是我的 .htaccess 文件设置

php_value memory_limit 128M
php_value post_max_size 500M
php_value upload_max_filesize 500M

下面的问题是,当我在本地主机上运行整个程序时,它按预期工作,但在服务器上,我从截击中得到了这个错误

BasicNetwork.performRequest: Unexpected response code 413 

Solved

问题出在服务器上,因为它是共享主机,文件上传限制受到限制。

最佳答案

就我而言,问题的答案是我使用的主机是共享主机。

而且帖子大小有限制,所以我们要用

更新.htaccess文件

post_max_size & upload_max_size

关于php - Volley BasicNetwork.performRequest : Unexpected response code 413,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37165874/

相关文章:

php - Symfony Flex : What does symfony. 锁定文件吗?

php - 类实例可以自毁吗?

android - 允许在 Android 上使用漂亮的 3X3 数字键盘输入负小数

android - 如何让android构建系统在应用程序中嵌入annotations.jar?

android - 我应该使用 Google Volley 而不是 AsyncTask

php - 如果名称中包含值,则删除 mySQLi 数据库中的表

php - 使用 PHP 提取数组键和值

android - 检测短信传入和传出

android - 存储 Volley 请求数据

android - com.android.volley.ServerError 错误 instanceof VolleyError