在android

时间:2018-01-04 04:34:05

标签: android file-upload upload android-volley zipfile

我必须上传一个文件,还有三个参数string。文件可以是图片或zipvolley怎样才能上传{{1}单击按钮时会提交file个参数吗?

我也试过这个链接 - https://www.simplifiedcoding.net/upload-pdf-file-server-android/        请帮帮我。谢谢!!               我的代码是:

string

2 个答案:

答案 0 :(得分:1)

试试这个

 public void fileUploadFunction() {

    // Getting file path using Filepath class.
    Pdfuri = FilePath.getPath(this, uri);
    Log.d("Pdfuri", Pdfuri);

    // If file path object is null then showing toast message to move file into internal storage.
    if (Pdfuri == null) {

        Toast.makeText(this, "Please move your PDF file to internal storage & try again.", Toast.LENGTH_LONG).show();

    }
    // If file path is not null then PDF uploading file process will starts.
    else {

        try {

            PdfID = UUID.randomUUID().toString();


            new MultipartUploadRequest(this, PdfID, AppConstants.URL)
                            .addFileToUpload(Pdfuri, "pdf")
                            .addParameter("course", course.trim())
                            .addParameter("course_id", c_id.trim())
                            .addParameter("stream", stream.trim())
                            .setNotificationConfig(new UploadNotificationConfig())
                            .setMaxRetries(5)
                            .startUpload();
            Toast.makeText(MainActivity.this,"Successfully Uploaded",Toast.LENGTH_SHORT).show();

        } catch (Exception exception) {

            Toast.makeText(this, 
                   exception.getMessage(),Toast.LENGTH_SHORT).show();
        }
     }
 }

答案 1 :(得分:0)

VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
    @Override
    public void onResponse(NetworkResponse response) {
        //Read response here
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {                
        error.printStackTrace();
    }
}) {
    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        //Add post values here
        return params;
    }

    @Override
    protected Map<String, DataPart> getByteData() {
        Map<String, DataPart> params = new HashMap<>();
        // Add you file here
        return params;
    }
};

VolleySingleton.getInstance(getBaseContext()).addToRequestQueue(multipartRequest);

您可以使用multipart。这是一个示例代码