我正在尝试为Volley自定义请求构建一个帖子URL,使用带有普通param和数组参数的post方法将一些数据发布到服务器。但是在为请求构建post url时我遇到了一些异常。我在使用排球自定义请求发出请求之前会遇到异常。
Map<String, String> params = new HashMap<>();
params.put(API.Parameter.ANDROID_DEVICE_ID, appManager.getDeviceId());
params.put(API.Parameter.ANDROID_APP_VERSION, appManager.getAppVersion());
params.put("trip_no", MainActivity.temp_trip_id);
Map<String, List<String>> arrayParams = new HashMap<>();
arrayParams.put("ticket_id",ticket_id);
arrayParams.put("ticket_name",ticket_name);
arrayParams.put("ticket_price_each",ticket_price_each);
PostUrlBuilder urlBuilder = new PostUrlBuilder(API.BOOK_TRIP_API_URL, params, arrayParams);
String Url = urlBuilder.getQueryUrl();
ObjectRequest<OnlineUserData> onlineUserDataObjectRequest = new ObjectRequest<OnlineUserData>(Request.Method.POST, Url, null,
new Response.Listener<OnlineUserData>() {
@Override
public void onResponse(OnlineUserData response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}, OnlineUserData.class);
AppController.getInstance().addToRequestQueue(onlineUserDataObjectRequest);
我的PostUrlBuilder.java类是:
public class PostUrlBuilder {
private static final String TAG = PostUrlBuilder.class.getSimpleName();
private String url;
private Map<String, String> parameters;
private Map<String, List<String>> arrayParameters;
private String queryUrl;
public PostUrlBuilder() {
}
public PostUrlBuilder(String url, Map<String, String> parameters, Map<String, List<String>> arrayParameters) {
this.url = url;
this.parameters = parameters;
this.arrayParameters = arrayParameters;
buildQueryUrl();
}
public PostUrlBuilder(Map<String, String> parameters, String url) {
this.parameters = parameters;
this.url = url;
}
private void buildQueryUrl() {
StringBuilder urlBuilder = new StringBuilder(url);
if (parameters != null && parameters.size() != 0) {
urlBuilder.append("?");
int i = 0;
for (String key : parameters.keySet()) {
String value = parameters.get(key);
Log.d(TAG, "key= " + key + " & " + "value=" + value);
try {
urlBuilder.append(String.format("%s=%s", key, URLEncoder.encode(value, "UTf-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
if (i != parameters.size()) {
urlBuilder.append("&");
}
}
}
if (arrayParameters != null && arrayParameters.size() != 0) {
if (parameters != null && parameters.size() != 0) {
urlBuilder.append("&");
} else {
urlBuilder.append("?");
}
int i = 0;
for (String key : arrayParameters.keySet()) {
List<String> values = arrayParameters.get(key);
int j = 0;
for (String value : values) {
Log.d(TAG, "size = " + values.size() + " j = " + j + " key= " + key + " & " + "value=" + value);
try {
urlBuilder.append(String.format("%s[]=%s", key, URLEncoder.encode(value, "UTf-8")));
j++;
if (j != values.size()) {
urlBuilder.append("&");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
i++;
if (i != arrayParameters.size()) {
urlBuilder.append("&");
}
}
}
queryUrl = urlBuilder.toString();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, String> getParameters() {
return parameters;
}
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}
public String getQueryUrl() {
return queryUrl;
}
public void setQueryUrl(String queryUrl) {
this.queryUrl = queryUrl;
}
public Map<String, List<String>> getArrayParameters() {
return arrayParameters;
}
public void setArrayParameters(Map<String, List<String>> arrayParameters) {
this.arrayParameters = arrayParameters;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PostUrlBuilder)) return false;
PostUrlBuilder that = (PostUrlBuilder) o;
return this.getUrl().equals(that.getUrl())
&& this.getParameters().equals(that.getParameters())
&& this.getArrayParameters().equals(that.getArrayParameters())
&& this.getQueryUrl().equals(that.getQueryUrl());
}
@Override
public int hashCode() {
int result = getUrl().hashCode();
result = 31 * result + getParameters().hashCode();
result = 31 * result + getArrayParameters().hashCode();
result = 31 * result + getQueryUrl().hashCode();
return result;
}
@Override
public String toString() {
return "GetUrlBuilder{" +
"url='" + url + '\'' +
", parameters=" + parameters +
", queryUrl='" + queryUrl + '\'' +
'}';
}
}
我在下面排除了例外:
urlBuilder.append(String.format("%s[]=%s", key, URLEncoder.encode(value, "UTf-8")));
错误Log-cat显示以下错误:
FATAL EXCEPTION: main
Process: com.shohoz.launch.cabin, PID: 12230
java.lang.NullPointerException
at libcore.net.UriCodec.encode(UriCodec.java:132)
at java.net.URLEncoder.encode(URLEncoder.java:57)
at com.shohoz.launch.cabin.toolbox.PostUrlBuilder.buildQueryUrl(PostUrlBuilder.java:70)
at com.shohoz.launch.cabin.toolbox.PostUrlBuilder.<init>(PostUrlBuilder.java:29)
at com.shohoz.launch.cabin.fragment.RightFullSeatLayoutFragment.onlineCreateAndInsertIntoConfirm(RightFullSeatLayoutFragment.java:1105)
at com.shohoz.launch.cabin.fragment.RightFullSeatLayoutFragment$15.onClick(RightFullSeatLayoutFragment.java:857)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680)
at dalvik.system.NativeStart.main(Native Method)
提前致谢。
我已经找出arrayParams.put("ticket_price_each",ticket_price_each);
发送空值。这就是应用程序崩溃的原因。