使用Koush Ion库上传多部分文件 [英] Uploading multipart file with Koush Ion library
问题描述
在我上一个应用程序中,我将使用 Koush Ion 库。这是如此方便,但我有上传文件到我的其余服务器的问题。
注意:我的服务器对成功上传的响应过程是1 / b
我的代码我喜欢这个:
public class MainActivity extends Activity {
Button upload,login;
TextView uploadCount;
ProgressBar progressBar;
字符串标记,FilePath;
未来< String>上传;
@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;
$ b upload.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
if(uploading != null&!uploading.isCancelled()){
resetUpload();
return;
}
$ b $ upload.setText(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(){
@覆盖
public void onProgress(int downloaded,int total){
uploadCount.setText(+ downloaded +/+ tot人);
$ b $)
.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自动生成的方法存根
}
})
;
}
});
但是我从服务器得到了TimeoutException。我的问题是:
1.选择文件的方式是否正确?
2.应该使用Future Callback作为字符串吗?!
通过fiddler2检查我的请求到服务器,当我尝试上传文件到服务器...它显示我请求发送,multipartParameters发送,但是当试图发送文件... fiddler显示我的错误:
协议违规报告:
Content-Length不匹配:Request Header表示455字节,但客户端发送387字节。
我真的为我工作,下面是我的代码:
最终文件fileToUpload = new File(localFilePath);
。(上下文)
.load(Urls.UPLOAD_PICTURE)
.uploadProgressHandler(new ProgressCallback(){
@Override
public void onProgress(long uploaded,
mNotifyManager.notify(notificationId,mBuilder.build());
mBuilder.setProgress((int)total,( int)上传,false);
}
})
.setTimeout(60 * 60 * 1000)
.setMultipartFile(upload,image / jpeg,fileToUpload)
.asJsonObject()
//完成时运行回调
.setCallback(new FutureCallback< JsonObject>(){
@Override $ b $ public void onCompleted ,JsonObject结果){
//循环结束时,更新通知
mBuilder.setContentText(上传完成)
//删除进度条
.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(上下文,File upload complete,Toast.LENGTH_LONG).show();
}
});
}
希望能帮到别人:)
In my last app I'm going to use Koush Ion library. It's so handy but i have a problem with uploading file to my rest server. note: My server response to success upload Process is 1
My code i like this:
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
}
})
;
}
});
But I got TimeoutException from server. my questions are: 1. Is it right way for selecting file that I've done?! 2. Should is use Future Callback as String?!
I check my request to server by fiddler2, when I try to upload file to server...it show me that request send, multipartParameters send but when try to send file...fiddler show me the error:
Protocol Violation Report:
Content-Length mismatch: Request Header indicated 455 bytes, but client sent 387 bytes.
I actually works for me, Here is my code:
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();
}
});
}
Hope it helps someone :)
这篇关于使用Koush Ion库上传多部分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!