use of org.xutils.http.RequestParams in project xUtils3 by wyouflf.
the class HttpFragment method onTest2Click.
// 上传多文件示例
@Event(value = R.id.btn_test2)
private void onTest2Click(View view) {
RequestParams params = new RequestParams("http://192.168.0.13:8080/upload");
// 加到url里的参数, http://xxxx/s?wd=xUtils
params.addQueryStringParameter("wd", "xUtils");
// 添加到请求body体的参数, 只有POST, PUT, PATCH, DELETE请求支持.
// params.addBodyParameter("wd", "xUtils");
// 使用multipart表单上传文件
params.setMultipart(true);
params.addBodyParameter("file", new File("/sdcard/test.jpg"), // 如果文件没有扩展名, 最好设置contentType参数.
null);
try {
params.addBodyParameter("file2", new FileInputStream(new File("/sdcard/test2.jpg")), "image/jpeg", // 测试中文文件名
"你+& \" 好.jpg");
// InputStream参数获取不到文件名, 最好设置, 除非服务端不关心这个参数.
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
x.http().post(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onFinished() {
}
});
}
use of org.xutils.http.RequestParams in project xUtils3 by wyouflf.
the class DownloadManager method startDownload.
public synchronized void startDownload(String url, String label, String savePath, boolean autoResume, boolean autoRename, DownloadViewHolder viewHolder) throws DbException {
String fileSavePath = new File(savePath).getAbsolutePath();
DownloadInfo downloadInfo = db.selector(DownloadInfo.class).where("label", "=", label).and("fileSavePath", "=", fileSavePath).findFirst();
if (downloadInfo != null) {
DownloadCallback callback = callbackMap.get(downloadInfo);
if (callback != null) {
if (viewHolder == null) {
viewHolder = new DefaultDownloadViewHolder(null, downloadInfo);
}
if (callback.switchViewHolder(viewHolder)) {
return;
} else {
callback.cancel();
}
}
}
// create download info
if (downloadInfo == null) {
downloadInfo = new DownloadInfo();
downloadInfo.setUrl(url);
downloadInfo.setAutoRename(autoRename);
downloadInfo.setAutoResume(autoResume);
downloadInfo.setLabel(label);
downloadInfo.setFileSavePath(fileSavePath);
db.saveBindingId(downloadInfo);
}
// start downloading
if (viewHolder == null) {
viewHolder = new DefaultDownloadViewHolder(null, downloadInfo);
} else {
viewHolder.update(downloadInfo);
}
DownloadCallback callback = new DownloadCallback(viewHolder);
callback.setDownloadManager(this);
callback.switchViewHolder(viewHolder);
RequestParams params = new RequestParams(url);
params.setAutoResume(downloadInfo.isAutoResume());
params.setAutoRename(downloadInfo.isAutoRename());
params.setSaveFilePath(downloadInfo.getFileSavePath());
params.setExecutor(executor);
params.setCancelFast(true);
Callback.Cancelable cancelable = x.http().get(params, callback);
callback.setCancelable(cancelable);
callbackMap.put(downloadInfo, callback);
if (downloadInfoList.contains(downloadInfo)) {
int index = downloadInfoList.indexOf(downloadInfo);
downloadInfoList.remove(downloadInfo);
downloadInfoList.add(index, downloadInfo);
} else {
downloadInfoList.add(downloadInfo);
}
}
use of org.xutils.http.RequestParams in project xUtils3 by wyouflf.
the class ImageLoader method createRequestParams.
private static RequestParams createRequestParams(String url, ImageOptions options) {
RequestParams params = new RequestParams(url);
params.setCacheDirName(DISK_CACHE_DIR_NAME);
params.setConnectTimeout(1000 * 8);
params.setPriority(Priority.BG_LOW);
params.setExecutor(EXECUTOR);
params.setCancelFast(true);
params.setUseCookie(false);
if (options != null) {
ImageOptions.ParamsBuilder paramsBuilder = options.getParamsBuilder();
if (paramsBuilder != null) {
params = paramsBuilder.buildParams(params, options);
}
}
return params;
}
use of org.xutils.http.RequestParams in project xUtils3 by wyouflf.
the class ImageLoader method doLoad.
/**
* load from Network or DiskCache
*
* @param view
* @param url
* @param options
* @param callback
*/
private Cancelable doLoad(ImageView view, String url, ImageOptions options, Callback.CommonCallback<Drawable> callback) {
this.viewRef = new WeakReference<ImageView>(view);
this.options = options;
this.key = new MemCacheKey(url, options);
this.callback = callback;
if (callback instanceof Callback.ProgressCallback) {
this.progressCallback = (Callback.ProgressCallback<Drawable>) callback;
}
if (callback instanceof Callback.PrepareCallback) {
this.prepareCallback = (Callback.PrepareCallback<File, Drawable>) callback;
}
if (callback instanceof Callback.CacheCallback) {
this.cacheCallback = (Callback.CacheCallback<Drawable>) callback;
}
// set loadingDrawable
Drawable loadingDrawable = null;
if (options.isForceLoadingDrawable()) {
loadingDrawable = options.getLoadingDrawable(view);
view.setScaleType(options.getPlaceholderScaleType());
view.setImageDrawable(new AsyncDrawable(this, loadingDrawable));
} else {
loadingDrawable = view.getDrawable();
view.setImageDrawable(new AsyncDrawable(this, loadingDrawable));
}
// request
RequestParams params = createRequestParams(url, options);
if (view instanceof FakeImageView) {
synchronized (FAKE_IMG_MAP) {
FAKE_IMG_MAP.put(url, (FakeImageView) view);
}
}
return cancelable = x.http().get(params, this);
}
Aggregations