Skip to content

Commit

Permalink
一处请求url处理优化
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed Mar 19, 2022
1 parent 0094b41 commit c8e6fd6
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions xutils/src/main/java/org/xutils/http/request/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.TargetApi;
import android.os.Build;
import android.text.TextUtils;

import org.xutils.cache.DiskCacheEntity;
import org.xutils.cache.LruDiskCache;
import org.xutils.common.util.IOUtil;
Expand All @@ -16,30 +15,16 @@
import org.xutils.http.body.RequestBody;
import org.xutils.http.cookie.DbCookieStore;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TimeZone;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.util.*;

/**
* Created by wyouflf on 15/7/23.
Expand All @@ -66,13 +51,15 @@ public HttpRequest(RequestParams params, Type loadType) throws Throwable {
protected String buildQueryUrl(RequestParams params) throws IOException {
String uri = params.getUri();
StringBuilder queryBuilder = new StringBuilder(uri);
if (!uri.contains("?")) {
queryBuilder.append("?");
} else if (!uri.endsWith("?")) {
queryBuilder.append("&");
}
List<KeyValue> queryParams = params.getQueryStringParams();
if (queryParams != null) {

if (queryParams != null && !queryParams.isEmpty()) {
if (!uri.contains("?")) {
queryBuilder.append("?");
} else if (!uri.endsWith("?")) {
queryBuilder.append("&");
}

for (KeyValue kv : queryParams) {
String name = kv.key;
String value = kv.getValueStrOrNull();
Expand All @@ -83,15 +70,16 @@ protected String buildQueryUrl(RequestParams params) throws IOException {
.append("&");
}
}
}

if (queryBuilder.charAt(queryBuilder.length() - 1) == '&') {
queryBuilder.deleteCharAt(queryBuilder.length() - 1);
}
if (queryBuilder.charAt(queryBuilder.length() - 1) == '&') {
queryBuilder.deleteCharAt(queryBuilder.length() - 1);
}

if (queryBuilder.charAt(queryBuilder.length() - 1) == '?') {
queryBuilder.deleteCharAt(queryBuilder.length() - 1);
if (queryBuilder.charAt(queryBuilder.length() - 1) == '?') {
queryBuilder.deleteCharAt(queryBuilder.length() - 1);
}
}

return queryBuilder.toString();
}

Expand Down

1 comment on commit c8e6fd6

@xiyangyang8110
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像是我提报的问题,哈哈

Please sign in to comment.