繁体   English   中英

使用Koush Ion库上传多部分文件

[英]Uploading multipart file with Koush Ion library

在我的上一个应用程序中,我将使用Koush Ion库。 它非常方便但我将文件上传到我的休息服务器时遇到了问题。 注意:我的服务器对成功上传过程的响应是1

我的代码我喜欢这个:

public class MainActivity extends Activity {

    Button upload, login;
    TextView uploadCount;
    ProgressBar progressBar;
    String token, FilePath;

    Future<String> uploading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        upload      = (Button) findViewById(R.id.upload);
        uploadCount = (TextView) findViewById(R.id.upload_count);
        progressBar = (ProgressBar) findViewById(R.id.progress);
        token       = "147c85ce29dc585966271280d59899a02b94c020";
        FilePath    = Environment.getExternalStorageDirectory().toString()+"/police.mp3";


        upload.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (uploading !=null && !uploading.isCancelled()){
                    resetUpload();
                    return;
                }

                upload.setText("Uploading...");

                uploading = Ion.with(MainActivity.this)
                            .load("http://myserver.com/api/v1/tone/upload/?token="+token)
                            .setLogging("UPLOAD LOGS:", Log.DEBUG)
                            .uploadProgressBar(progressBar)
                            .uploadProgressHandler(new ProgressCallback() {

                                @Override
                                public void onProgress(int downloaded, int total) {
                                    uploadCount.setText("" + downloaded + "/" + total);

                                }
                            })
                            .setMultipartParameter("title", "police")
                            .setMultipartParameter("category", "7")
                            .setMultipartFile("file_url", new File(FilePath))
                            .asString()
                            .setCallback( new FutureCallback<String>() {

                                @Override
                                public void onCompleted(Exception e, String result) {
                                    // TODO Auto-generated method stub

                                }
                            })
                            ;
            }
        });

但我从服务器得到了TimeoutException。 我的问题是:1。选择我做过的文件是正确的吗?! 2.应该使用Future Callback作为字符串吗?!

使用webform将数据发布到Web服务器

我通过fiddler2检查我的服务器请求,当我尝试将文件上传到服务器时......它向我显示请求发送,multipartParameters发送但是当尝试发送文件时... fiddler告诉我错误:

Protocol Violation Report:
Content-Length mismatch: Request Header indicated 455 bytes, but client sent 387 bytes.

我实际上为我工作,这是我的代码:

final File fileToUpload = new File(localFilePath);
Ion.with(context)
            .load(Urls.UPLOAD_PICTURE)
            .uploadProgressHandler(new ProgressCallback() {
                @Override
                public void onProgress(long uploaded, long total) {
                    // Displays the progress bar for the first time.
                    mNotifyManager.notify(notificationId, mBuilder.build());
                    mBuilder.setProgress((int) total, (int) uploaded, false);
                }
            })
            .setTimeout(60 * 60 * 1000)
            .setMultipartFile("upload", "image/jpeg", fileToUpload)
            .asJsonObject()
                    // run a callback on completion
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
                    // When the loop is finished, updates the notification
                    mBuilder.setContentText("Upload complete")
                            // Removes the progress bar
                            .setProgress(0, 0, false);
                    mNotifyManager.notify(notificationId, mBuilder.build());
                    if (e != null) {
                        Toast.makeText(context, "Error uploading file", Toast.LENGTH_LONG).show();
                        return;
                    }
                    Toast.makeText(context, "File upload complete", Toast.LENGTH_LONG).show();
                }
            });
}

希望它可以帮助别人:)

搜索了很长时间,并注意到重要的是在.load()中包含POST

Ion.with(getBaseContext()).load("POST",url).uploadProgressHandler(new ProgressCallback()
        {
            @Override
            public void onProgress(long uploaded, long total)
            {
                System.out.println("uploaded " + (int)uploaded + " Total: "+total);
            }
        }).setMultipartParameter("platform", "android").setMultipartFile("image", new File(getPath(selectedImage))).asString().setCallback(new FutureCallback<String>()
        {
            @Override
            public void onCompleted(Exception e, String result)
            {

            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM