将图像上传到服务器时(上传时出错)

时间:2016-12-23 17:53:19

标签: image file-upload server android-volley multipartentity

在我的应用程序中,我发送3个参数到服务器纬度,经度和图像。早期我使用凌空发送参数,但由于我有一个图像,我不得不在我的代码中使用Multipart。但我得到一个错误而uploadind。在通知栏中,上传开始,但经过一段时间后,上传中显示错误 以下是MultiPart的代码:

public void send() {
            try {
                String uploadId = UUID.randomUUID().toString();


                //Creating a multi part request
                new MultipartUploadRequest(this, uploadId, REGISTER_URL)
                        .setMethod("POST")
                        .addParameter("action", "location")
                        .addFileToUpload(imagePath, "data")//Adding file
                        //.addParameter("name", name) //Adding text parameter to the request
                        .setNotificationConfig(new UploadNotificationConfig())
                        .setMaxRetries(5)
                        .startUpload(); //Starting the upload

            } catch (Exception exc) {
                Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }

以下是我的截击码:

    final String latitudee = String.valueOf(latitude);
    final String longitudee =String.valueOf(longitude);
    final String datae = imagePath;
    //getting the actual path of the image

   StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(MapsActivity.this,response,Toast.LENGTH_LONG).show();
                    System.out.println(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MapsActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("action","location");
            params.put("latitude",latitudee);
            params.put("longitude",longitudee);
            send();
           // params.put("data", datae);
            //Uploading code




            return params;}
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

请在我出错的地方帮助我

1 个答案:

答案 0 :(得分:0)

您也可以通过Multipart请求库发送您的其他参数。只需添加&#34;添加参数&#34;发送更多参数。