【发布时间】:2014-05-20 04:11:41
【问题描述】:
我正在使用以下代码通过 MultipartEntityBuilder 将照片上传到我的服务器。最后某处写着// FIXME Put your progress bar stuff here!。我不知道是否应该将整个函数放入 Asynctask 中,或者在上传文件时应该如何显示进度。
我检查了Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar,但它并没有真正帮助。
public String postFile(String fileName) throws Exception {
fileName = "/mnt/sdcard/DCIM/100MEDIA/IMAG0309.jpg";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.mywebsite.com/upload_file.php");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
final File file = new File(fileName);
FileBody fb = new FileBody(file);
builder.addPart("file",fb);
String userid = session_userid;
builder.addTextBody("userID", userid);
final HttpEntity yourEntity = builder.build();
class ProgressiveEntity implements HttpEntity {
@Override
public void consumeContent() throws IOException {
yourEntity.consumeContent();
}
@Override
public InputStream getContent() throws IOException,
IllegalStateException {
return yourEntity.getContent();
}
@Override
public Header getContentEncoding() {
return yourEntity.getContentEncoding();
}
@Override
public long getContentLength() {
return yourEntity.getContentLength();
}
@Override
public Header getContentType() {
return yourEntity.getContentType();
}
@Override
public boolean isChunked() {
return yourEntity.isChunked();
}
@Override
public boolean isRepeatable() {
return yourEntity.isRepeatable();
}
@Override
public boolean isStreaming() {
return yourEntity.isStreaming();
} // CONSIDER put a _real_ delegator into here!
@Override
public void writeTo(OutputStream outstream) throws IOException {
class ProxyOutputStream extends FilterOutputStream {
public ProxyOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(int idx) throws IOException {
out.write(idx);
}
public void write(byte[] bts) throws IOException {
out.write(bts);
}
public void write(byte[] bts, int st, int end) throws IOException {
out.write(bts, st, end);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
out.close();
}
}
class ProgressiveOutputStream extends ProxyOutputStream {
int totalSent;
public ProgressiveOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(byte[] bts, int st, int end) throws IOException {
// FIXME Put your progress bar stuff here!
out.write(bts, st, end);
}
}
yourEntity.writeTo(new ProgressiveOutputStream(outstream));
}
};
ProgressiveEntity myEntity = new ProgressiveEntity();
post.setEntity(myEntity);
HttpResponse response = client.execute(post);
return getContent(response);
}
这是我所做的,但我仍然需要你们完成它:
@Override
protected String doInBackground(HttpResponse... arg0)
{
String fileName = "/mnt/sdcard/DCIM/100MEDIA/IMAG0309.jpg";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.mywebsite.com/upload_file.php");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
final File file = new File(fileName);
FileBody fb = new FileBody(file);
builder.addPart("file",fb);
String userid = session_userid;
builder.addTextBody("userID", userid);
final HttpEntity yourEntity = builder.build();
class ProgressiveEntity implements HttpEntity {
@Override
public void consumeContent() throws IOException {
yourEntity.consumeContent();
}
@Override
public InputStream getContent() throws IOException,
IllegalStateException {
return yourEntity.getContent();
}
@Override
public Header getContentEncoding() {
return yourEntity.getContentEncoding();
}
@Override
public long getContentLength() {
return yourEntity.getContentLength();
}
@Override
public Header getContentType() {
return yourEntity.getContentType();
}
@Override
public boolean isChunked() {
return yourEntity.isChunked();
}
@Override
public boolean isRepeatable() {
return yourEntity.isRepeatable();
}
@Override
public boolean isStreaming() {
return yourEntity.isStreaming();
} // CONSIDER put a _real_ delegator into here!
@Override
public void writeTo(OutputStream outstream) throws IOException {
class ProxyOutputStream extends FilterOutputStream {
public ProxyOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(int idx) throws IOException {
out.write(idx);
}
public void write(byte[] bts) throws IOException {
out.write(bts);
}
public void write(byte[] bts, int st, int end) throws IOException {
out.write(bts, st, end);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
out.close();
}
}
class ProgressiveOutputStream extends ProxyOutputStream {
public ProgressiveOutputStream(OutputStream proxy) {
super(proxy);
int totalSent = 0;
}
public void write(byte[] bts, int st, int end) throws IOException {
// FIXME Put your progress bar stuff here!
//what's totalSize?
publishProgress((int) ((end / (float) totalSize) * 100));
out.write(bts, st, end);
}
}
yourEntity.writeTo(new ProgressiveOutputStream(outstream));
}
};
ProgressiveEntity myEntity = new ProgressiveEntity();
post.setEntity(myEntity);
HttpResponse response = client.execute(post);
return getContent(response);
//Here it says I should put this into a try catch but then I get an error on
// the doInBackground line saying it needs to return a string
}
protected void onProgressUpdate(Integer... progress) {
pd.setProgress((int) (progress[0]));
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String file_url) {
pd.dismiss();
connection.disconnect();
Toast.makeText(getApplicationContext(), "Ready.", Toast.LENGTH_LONG).show();
}
}
【问题讨论】:
-
你可以把它包装在一个 AsyncTask 中,用 onPrexcute() 显示一个进度条,然后在 onPostExecute() 中隐藏进度条……假设你只关心一个不确定的进度条。
-
不是真的...我想显示进度。顺便说一句,你有不确定进度条的示例代码吗?之前我使用了更简单的 DataOutputStream 来通过 Asynctask 上传文件,同时显示进度,所以我可能会弄清楚。
-
只需使用带有 android:indeterminateDrawable="@drawable/yourDrawable" 属性的
。 -
@erdomester 你找到解决方案了吗?
-
我不明白这个问题,因为你不能从 Android 的 UI 线程 HTTP(或 any *TP),因此你需要一个后台线程,因此您需要 AsyncTask 所做的一切,所以只需使用它。换句话说,“ProgressBar”的问题?和“AsyncTask?”与上传文件的问题脱节,因此如果您使用他们的示例开始使用它们,并且仅 then 放入 HTTP 内容(并且仅 THEN 放入 ProgressiveEntity),事情可能会失败为您准备好。