use of cn.jeesoft.qa.libcore.http.part.Part in project QuickAndroid by ImKarl.
the class OkHttp method parseGetUrl.
/**
* 解析GET方式URL(补充URL请求参数)
*
* @param url
* @param params
* @return 返回处理后的URL
*/
private static String parseGetUrl(String url, Map<String, List<Part>> params) {
StringBuilder sb = new StringBuilder();
if (params == null) {
params = new HashMap<String, List<Part>>();
}
if (url.contains("?")) {
if (url.endsWith("&")) {
sb.append(url.substring(0, url.length() - 1));
}
} else {
sb.append(url + "?");
}
for (Entry<String, List<Part>> entry : params.entrySet()) {
String name = entry.getKey();
List<Part> parts = entry.getValue();
try {
if (!TextUtils.isEmpty(name) && !QAStringUtils.isEmpty(parts)) {
for (Part part : parts) {
if (part instanceof StringPart) {
sb.append("&" + URLEncoder.encode(part.name(), "UTF-8") + "=" + URLEncoder.encode(((StringPart) part).value(), "UTF-8"));
}
}
}
} catch (UnsupportedEncodingException e) {
QALog.e(e);
}
}
return sb.toString().replace("?&", "?");
}
use of cn.jeesoft.qa.libcore.http.part.Part in project QuickAndroid by ImKarl.
the class OkHttp method createRequest.
private <T> Request createRequest(QAHttpMethod method, String url, QARequestParams params, final QAHttpCallback<T> listener) {
if (method == null) {
method = QAHttpMethod.GET;
}
final String finalUrl = url;
if (method == QAHttpMethod.GET) {
url = parseGetUrl(url, params != null ? params.getParams() : null);
params = null;
} else {
if (params == null) {
params = new QARequestParams();
}
}
Request.Builder builder = new Request.Builder();
try {
builder.url(url);
} catch (final Exception e) {
sendFailedCallback(finalUrl, e, listener);
return null;
}
// 强制使用缓存
builder.cacheControl(CacheControl.FORCE_CACHE);
// header
Headers.Builder headerBuilder = new Headers.Builder();
if (params != null && !params.getHeaders().isEmpty()) {
for (Entry<String, String> entry : params.getHeaders().entrySet()) {
headerBuilder.add(entry.getKey(), entry.getValue());
}
}
builder.headers(headerBuilder.build());
// param
RequestBody requestBody = null;
if (params != null) {
if (!TextUtils.isEmpty(params.getBody())) {
requestBody = RequestBody.create(MEDIA_TYPE_TEXT, params.getBody());
} else {
try {
MultipartBuilder paramBuilder = new MultipartBuilder();
if (params != null && !params.getParams().isEmpty()) {
for (Entry<String, List<Part>> entry : params.getParams().entrySet()) {
String name = entry.getKey();
List<Part> parts = entry.getValue();
if (parts != null && !parts.isEmpty()) {
for (Part part : parts) {
paramBuilder.addPart(part.header(), part.body());
}
}
}
}
requestBody = paramBuilder.build();
} catch (IllegalStateException e) {
requestBody = new FormEncodingBuilder().build();
}
}
}
builder.method(method.name(), requestBody == null ? null : new ProgressRequestBody(requestBody, new OnProgressListener() {
@Override
public void onProgress(long currentBytes, long contentLength) {
// 上传进度
sendProgressCallback(finalUrl, currentBytes, contentLength, QAHttpAction.REQUEST, listener);
}
}));
final Request request = builder.build();
mOnProgressListeners.put(request, new OnProgressListener() {
@Override
public void onProgress(long currentBytes, long contentLength) {
// 下载进度
sendProgressCallback(finalUrl, currentBytes, contentLength, QAHttpAction.RESPONSE, listener);
}
});
return request;
}
Aggregations