Android 进展中的loopj未按预期工作
从android应用程序上传带有loopj的文件时,我遇到了一个非常奇怪的onProgress行为。 我正在使用SyncHttpClient一个接一个地上传多个文件, 但是onPorgress总是被一些看起来像硬编码或固定数字的数字触发,而不是二进制资产和已上载字节的实际大小。 下面是我得到的示例输出Android 进展中的loopj未按预期工作,android,loopj,Android,Loopj,从android应用程序上传带有loopj的文件时,我遇到了一个非常奇怪的onProgress行为。 我正在使用SyncHttpClient一个接一个地上传多个文件, 但是onPorgress总是被一些看起来像硬编码或固定数字的数字触发,而不是二进制资产和已上载字节的实际大小。 下面是我得到的示例输出 V/AsyncHttpResponseHandler﹕ Progress 3353492 from 3353528 (100%) V/AsyncHttpResponseHandler﹕ Progr
V/AsyncHttpResponseHandler﹕ Progress 3353492 from 3353528 (100%)
V/AsyncHttpResponseHandler﹕ Progress 3353528 from 3353528 (100%)
V/AsyncHttpResponseHandler﹕ Progress 1338 from 2417 (55%)
V/AsyncHttpResponseHandler﹕ Progress 2417 from 2417 (100%)
然后,对于上传的每个文件,都会发生相同的调用顺序。任何想法都将不胜感激。顺便说一句,文件上传很好,我只是不能让负载指示器正常工作
以下是代码示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
waveOperation = new WaveOperation();
waveOperation.execute();
}
//inner asynchTask class
private class WaveOperation extends AsyncTask<Void, Object, String> {
@Override
protected String doInBackground(Void... arg0) {
waveAll(ApplicationContextProvider.getContext());
return "Executed";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
private void waveAll(Context context) {
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE,
MediaStore.Images.ImageColumns.ORIENTATION
};
String selection = MediaStore.Images.Media.DATE_TAKEN + " > ?";
String[] selectionArgs = {String.valueOf(ApplicationContextProvider.getCurrentAssetDateTime().getTime())};
final Cursor cursor = context.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection,
selectionArgs, MediaStore.Images.ImageColumns.DATE_TAKEN + " ASC");
while (cursor.moveToNext() && !isCancelled()) {
final String imageLocation = cursor.getString(1);
File imageFile = new File(imageLocation);
if (imageFile.exists()) { // is there a better way to do this?
Bitmap bm = BitmapFactory.decodeFile(imageLocation);
int orientation = cursor.getInt(5);
// Log.d("###################### orientation: ", String.valueOf(orientation));
long timeTaken = cursor.getLong(3);
final String dateTaken = simpleDateFormat.format(timeTaken);
...
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); // rotating bitmap
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
try {
EWImage.uploadPhoto(stream.toByteArray(), dateTaken + ".jpg", new AsyncHttpResponseHandler() {
//this is the method that is not being fired often enough during the upload
//and when it's called, it report some weird numbers that look always the same regardles
//of file being uploaded
@Override
public void onProgress(int bytesWritten, int totalSize) {
super.onProgress(bytesWritten, totalSize);
Log.d("--------------progress: ", String.valueOf(bytesWritten) + " of " + String.valueOf(totalSize));
}
@Override
public void onStart() {
super.onStart();
...
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
...
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
...
}
@Override
public void onFinish() {
super.onFinish();
}
}
);
} catch (FileNotFoundException e) {
Log.e("FileNotFound", e.toString());
e.printStackTrace();
}
}
}
}
}
以及在基类中定义如下的SYNC_HTTP_客户端:
protected final static SyncHttpClient SYNC_HTTP_CLIENT = new SyncHttpClient();
static {
PersistentCookieStore cookieStore = new PersistentCookieStore(ApplicationContextProvider.getContext());
SYNC_HTTP_CLIENT.setCookieStore(cookieStore);
}
我确实弄明白了,
它只适用于File参数,不适用于Stream或ByteArray——似乎是loopj中的一个bug。如果检查AsyncHttpResponseHandler的源代码,您会注意到在获得请求响应时也会调用“sendProgressMessage”。这就是您看到的最后两次更新:
V/AsyncHttpResponseHandler﹕ Progress 1338 from 2417 (55%)
V/AsyncHttpResponseHandler﹕ Progress 2417 from 2417 (100%)
关于第一次更新:
V/AsyncHttpResponseHandler﹕ Progress 3353492 from 3353528 (100%)
V/AsyncHttpResponseHandler﹕ Progress 3353528 from 3353528 (100%)
请记住,totalSize中可能还考虑了任何请求头。如果您只想打印文件大小,那么为什么不在onSuccess中打印文件大小?发布几部分代码我不想打印文件大小,而是在上传时打印上传进度。发布您的部分代码。在没有看到代码的情况下,我们无法提供任何建议,您可以在onSuccess()中打印您想要的内容。刚刚用代码示例更新了原始帖子。这个问题解决了吗?同样的问题是
totalSize
始终为1
V/AsyncHttpResponseHandler﹕ Progress 3353492 from 3353528 (100%)
V/AsyncHttpResponseHandler﹕ Progress 3353528 from 3353528 (100%)