com.mycomm.YesHttp.core.JsonRequest Maven / Gradle / Ivy
/*
* Copyright 2018 jw362j.
*
* 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.mycomm.YesHttp.core;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;
/**
*
* @author jw362j
*/
public abstract class JsonRequest extends BaseRequest {
private final HttpObserver observer = new HttpObserver() {
public void Observe(URLConnection connection) {
if (Method.POST != getmMethod()) {
return;
}
if (connection == null) {
return;
}
connection.setRequestProperty("Content-Type", "application/json");
}
};
public JsonRequest(short mMethod, String mUrl, Response.Listener response, Response.ErrorListener errorListener, YesLog log, short protocol) {
super(mMethod, mUrl, response, errorListener, log, protocol);
}
public JsonRequest(String mUrl, Response.Listener response, Response.ErrorListener errorListener) {
super(mUrl, response, errorListener);
}
public JsonRequest(String mUrl, Response.Listener response) {
super(mUrl, response);
}
public JsonRequest(short mMethod, String mUrl, Response.Listener response, HttpObserver observer, Response.ErrorListener errorListener, YesLog log, short protocol) {
super(mMethod, mUrl, response, observer, errorListener, log, protocol);
}
@Override
public HttpObserver getHttpObserver() {
return observer;
}
public void write(OutputStreamWriter outputStreamWriter) {
try {
if (Method.POST != getmMethod()) {
return;
}
Map params = getParams();
if (params == null || params.isEmpty()) {
getLog().LogMe("Params are null in Method.POST about JsonRequest,post body output give up ...");
return;
}
String targetJsonData = JsonBodyBuilder();
if (targetJsonData != null && targetJsonData.length() > 0) {
getLog().LogMe("the post data in JsonRequest is:"+targetJsonData);
outputStreamWriter.write(targetJsonData);
return;
}
StringBuilder post_data_defaultJson = new StringBuilder();
post_data_defaultJson.append("{");
Set keys = params.keySet();
for (String key : keys) {
post_data_defaultJson.append("\"").append(key).append("\"").append(":").append("\"").append(params.get(key)).append("\"").append(",");
}
post_data_defaultJson.deleteCharAt(post_data_defaultJson.length() - 1);
post_data_defaultJson.append("}");
targetJsonData = post_data_defaultJson.toString();
getLog().LogMe("the post data in JsonRequestA is:"+targetJsonData);
outputStreamWriter.write(targetJsonData);
} catch (IOException ex) {
getLog().LogMe("Exception in StringRequest write:" + ex.getMessage());
}
}
public abstract String JsonBodyBuilder();//have to build the json format request body ,if none-null is returned here,then would ignore getParams() method
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy