修复重构loader后,http上传失败的问题

This commit is contained in:
laoyuyu
2020-01-08 19:42:16 +08:00
parent 432ae1c145
commit 7c4012c021
28 changed files with 255 additions and 106 deletions

View File

@@ -85,8 +85,7 @@ public final class HttpRecordHandler extends RecordHandler {
record.threadNum = threadNum;
int requestType = getWrapper().getRequestType();
if (requestType == ITaskWrapper.D_FTP || requestType == ITaskWrapper.D_FTP_DIR
|| requestType == ITaskWrapper.D_HTTP || requestType == ITaskWrapper.DG_HTTP) {
if (requestType == ITaskWrapper.D_HTTP || requestType == ITaskWrapper.DG_HTTP) {
record.isBlock = Configuration.getInstance().downloadCfg.isUseBlock();
} else {
record.isBlock = false;

View File

@@ -101,7 +101,7 @@ public final class HttpTaskOption implements ITaskOption {
}
public String getAttachment() {
return attachment;
return TextUtils.isEmpty(attachment) ? "file" : attachment;
}
public void setAttachment(String attachment) {

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.http;
package com.arialyy.aria.http.download;
import android.net.TrafficStats;
import android.net.Uri;
@@ -30,6 +30,8 @@ import com.arialyy.aria.core.processor.IHttpFileLenAdapter;
import com.arialyy.aria.exception.AriaIOException;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.exception.TaskException;
import com.arialyy.aria.http.ConnectionHelp;
import com.arialyy.aria.http.HttpTaskOption;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
@@ -55,7 +57,7 @@ import java.util.UUID;
/**
* 下载文件信息获取
*/
public final class HttpFileInfoTask implements IInfoTask, Runnable {
final class HttpDFileInfoTask implements IInfoTask, Runnable {
private static final String TAG = "HttpFileInfoThread";
private DownloadEntity mEntity;
private DTaskWrapper mTaskWrapper;
@@ -63,7 +65,7 @@ public final class HttpFileInfoTask implements IInfoTask, Runnable {
private Callback callback;
private HttpTaskOption taskOption;
public HttpFileInfoTask(DTaskWrapper taskWrapper) {
HttpDFileInfoTask(DTaskWrapper taskWrapper) {
this.mTaskWrapper = taskWrapper;
mEntity = taskWrapper.getEntity();
mConnectTimeOut = AriaConfig.getInstance().getDConfig().getConnectTimeOut();

View File

@@ -25,7 +25,6 @@ import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.ILoaderVisitor;
import com.arialyy.aria.exception.AriaIOException;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.http.HttpFileInfoTask;
import com.arialyy.aria.http.HttpTaskOption;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
@@ -98,7 +97,7 @@ public final class HttpDGInfoTask implements IInfoTask {
}
}
/*z
/*
* 获取组合任务大小,使用该方式获取到的组合任务大小,子任务不需要再重新获取文件大小
*/
private void getGroupSize() {
@@ -106,7 +105,7 @@ public final class HttpDGInfoTask implements IInfoTask {
@Override public void run() {
for (DTaskWrapper dTaskWrapper : wrapper.getSubTaskWrapper()) {
cloneHeader(dTaskWrapper);
HttpFileInfoTask infoTask = new HttpFileInfoTask(dTaskWrapper);
HttpDFileInfoTask infoTask = new HttpDFileInfoTask(dTaskWrapper);
infoTask.setCallback(subCallback);
mPool.execute(infoTask);
}

View File

@@ -23,7 +23,6 @@ import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.http.HttpFileInfoTask;
import com.arialyy.aria.http.HttpRecordHandler;
import com.arialyy.aria.http.HttpTaskOption;
@@ -45,7 +44,7 @@ public final class HttpDLoaderUtil extends AbsNormalLoaderUtil {
LoaderStructure structure = new LoaderStructure();
structure.addComponent(new HttpRecordHandler(getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new HttpFileInfoTask((DTaskWrapper) getTaskWrapper()))
.addComponent(new HttpDFileInfoTask((DTaskWrapper) getTaskWrapper()))
.addComponent(new HttpDTTBuilder(getTaskWrapper()));
structure.accept(getLoader());
return structure;

View File

@@ -18,10 +18,9 @@ package com.arialyy.aria.http.download;
import android.os.Handler;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.group.AbsSubDLoadUtil;
import com.arialyy.aria.core.group.SubRecordHandler;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.SubLoader;
import com.arialyy.aria.http.HttpFileInfoTask;
import com.arialyy.aria.http.HttpRecordHandler;
/**
* @Author lyy
@@ -48,9 +47,9 @@ final class HttpSubDLoaderUtil extends AbsSubDLoadUtil {
@Override protected LoaderStructure buildLoaderStructure() {
LoaderStructure structure = new LoaderStructure();
structure.addComponent(new HttpRecordHandler(getWrapper()))
structure.addComponent(new SubRecordHandler(getWrapper()))
.addComponent(new HttpDTTBuilder(getWrapper()))
.addComponent(new HttpFileInfoTask(getWrapper()));
.addComponent(new HttpDFileInfoTask(getWrapper()));
structure.accept(getLoader());
return structure;
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.http.upload;
import android.os.Handler;
import android.os.Looper;
import com.arialyy.aria.core.inf.IThreadStateManager;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.IInfoTask;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.loader.IThreadTaskBuilder;
import com.arialyy.aria.core.manager.ThreadTaskManager;
import com.arialyy.aria.core.task.IThreadTask;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.exception.AriaIOException;
import com.arialyy.aria.util.ALog;
import java.util.List;
final class HttpULoader extends AbsNormalLoader {
HttpULoader(UTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
}
@Override public void addComponent(IRecordHandler recordHandler) {
mRecordHandler = recordHandler;
}
/**
* @deprecated http 上传任务不需要设置这个
*/
@Deprecated
@Override public void addComponent(IInfoTask infoTask) {
}
@Override public void addComponent(IThreadStateManager threadState) {
mStateManager = threadState;
}
@Override public void addComponent(IThreadTaskBuilder builder) {
mTTBuilder = builder;
}
@Override protected void handleTask(Looper looper) {
mRecord = mRecordHandler.getRecord(getFileSize());
mStateManager.setLooper(mRecord, looper);
List<IThreadTask> tt = mTTBuilder.buildThreadTask(mRecord,
new Handler(looper, mStateManager.getHandlerCallback()));
if (tt == null || tt.isEmpty()) {
ALog.e(TAG, "创建线程任务失败");
getListener().onFail(false, new AriaIOException(TAG, "创建线程任务失败"));
return;
}
getListener().onStart(0);
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), tt.get(0));
startTimer();
}
@Override public long getFileSize() {
return mTaskWrapper.getEntity().getFileSize();
}
@Override protected void checkComponent() {
if (mRecordHandler == null) {
throw new NullPointerException("任务记录组件为空");
}
if (mStateManager == null) {
throw new NullPointerException("任务状态管理组件为空");
}
if (mTTBuilder == null) {
throw new NullPointerException("线程任务组件为空");
}
}
@Override public long getCurrentProgress() {
return mStateManager.getCurrentProgress();
}
}

View File

@@ -15,15 +15,13 @@
*/
package com.arialyy.aria.http.upload;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.http.HttpFileInfoTask;
import com.arialyy.aria.http.HttpRecordHandler;
import com.arialyy.aria.http.HttpTaskOption;
@@ -38,14 +36,14 @@ public final class HttpULoaderUtil extends AbsNormalLoaderUtil {
}
@Override public AbsNormalLoader getLoader() {
return mLoader == null ? new NormalLoader(getTaskWrapper(), getListener()) : mLoader;
return mLoader == null ? new HttpULoader((UTaskWrapper) getTaskWrapper(), getListener())
: mLoader;
}
@Override public LoaderStructure BuildLoaderStructure() {
LoaderStructure structure = new LoaderStructure();
structure.addComponent(new HttpRecordHandler(getTaskWrapper()))
.addComponent(new NormalThreadStateManager(getListener()))
.addComponent(new HttpFileInfoTask((DTaskWrapper) getTaskWrapper()))
.addComponent(new HttpUTTBuilder(getTaskWrapper()));
structure.accept(getLoader());
return structure;

View File

@@ -18,6 +18,7 @@ package com.arialyy.aria.http.upload;
import android.text.TextUtils;
import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.exception.AriaIOException;
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.exception.TaskException;
import com.arialyy.aria.http.BaseHttpThreadTaskAdapter;
@@ -102,7 +103,7 @@ final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
}
uploadFile(writer, mTaskOption.getAttachment(), uploadFile);
complete();
getEntity().setResponseStr(finish(writer));
} catch (Exception e) {
e.printStackTrace();
fail(new TaskException(TAG,
@@ -193,11 +194,6 @@ final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
inputStream.close();
writer.append(LINE_END);
writer.flush();
//if (getState().isCancel) {
// getState().isRunning = false;
// return;
//}
//getState().isRunning = false;
}
/**
@@ -210,7 +206,7 @@ final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
writer.append(LINE_END).flush();
writer.append(PREFIX).append(BOUNDARY).append(PREFIX).append(LINE_END);
writer.close();
writer.flush();
int status = mHttpConn.getResponseCode();
@@ -222,9 +218,11 @@ final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter {
}
reader.close();
mHttpConn.disconnect();
complete();
} else {
ALog.e(TAG, "response msg: " + mHttpConn.getResponseMessage() + "code: " + status);
// fail();
String msg = "response msg: " + mHttpConn.getResponseMessage() + "code: " + status;
ALog.e(TAG, msg);
fail(new AriaIOException(TAG, msg), false);
}
writer.flush();
writer.close();