Android 使用Koush Ion库上载多部分文件

Android 使用Koush Ion库上载多部分文件,android,file-upload,android-asynctask,ion-koush,Android,File Upload,Android Asynctask,Ion Koush,在我的上一个应用程序中,我将使用库。它非常方便,但我在将文件上载到rest服务器时遇到了问题。 注意:我的服务器对成功上载过程的响应为1 我的代码我喜欢这样: public class MainActivity extends Activity { Button upload, login; TextView uploadCount; ProgressBar progressBar; String token, FilePath; Future<

在我的上一个应用程序中,我将使用库。它非常方便,但我在将文件上载到rest服务器时遇到了问题。 注意:我的服务器对成功上载过程的响应为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

                                }
                            })
                            ;
            }
        });

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

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();
                }
            });
}
final File fileToUpload=新文件(localFilePath);
带(上下文)的离子
.load(URL.UPLOAD\u图片)
.uploadProgressHandler(新的ProgressCallback(){
@凌驾
公共void onProgress(长时间上传,长时间总计){
//第一次显示进度条。
mNotifyManager.notify(notificationId,mBuilder.build());
mBuilder.setProgress((int)总计,(int)上传,false);
}
})
.setTimeout(60*60*1000)
.setMultipartFile(“上载”、“图像/jpeg”、fileToUpload)
.asJsonObject()
//完成后运行回调
.setCallback(新的FutureCallback(){
@凌驾
未完成公共无效(异常e,JsonObject结果){
//循环完成后,更新通知
mBuilder.setContentText(“上传完成”)
//删除进度条
.setProgress(0,0,false);
mNotifyManager.notify(notificationId,mBuilder.build());
如果(e!=null){
Toast.makeText(上下文,“上传文件时出错”,Toast.LENGTH_LONG.show();
返回;
}
Toast.makeText(上下文,“文件上传完成”,Toast.LENGTH_LONG.show();
}
});
}

希望它能帮助某人:)

为此搜索了很长时间,并注意到重要的事情是将POST-in.load()包含在内

Ion.with(getBaseContext()).load(“POST”,url).uploadProgressHandler(new ProgressCallback())
{
@凌驾
公共void onProgress(长时间上传,长时间总计)
{
System.out.println(“上传”+(int)上传+”总计:“+总计);
}
}).setMultipartParameter(“平台”、“android”).setMultipartFile(“图像”,新文件(getPath(selectedImage))).asString().setCallback(新未来回调)()
{
@凌驾
未完成公共void(异常e,字符串结果)
{
}
});

什么版本的ion?你的服务器后端是什么?使用ion对服务器进行其他http调用吗?@koush最新的一个,是的,我可以进行http调用(我可以发送json对象并获得json对象作为结果)…请帮助我。非常感谢:)嘿,你解决这个问题了吗?我也一样thing@Danpe没有人,这是一个开放的问题,谢谢,现在我正在使用Loopj库作为我应用程序的这一部分,我将在第一次发布后使用你的解决方案。在URL.UPLOAD\u图片中,你是在服务器上使用脚本还是指向服务器上的文件夹?包含这一点似乎很合理,但对我来说不是必要的
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();
                }
            });
}
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)
            {

            }
        });