use of com.alibaba.fastjson.JSONObject in project weex-example by KalicyZhou.
the class WXDomStatementTest method testStartAnimation.
@Test
public void testStartAnimation() throws Exception {
createBody();
JSONObject obj;
obj = new JSONObject();
obj.put("type", "div");
obj.put("ref", "100");
stmt.addDom(obj, WXDomObject.ROOT, 0);
stmt.startAnimation("100", "", null);
}
use of com.alibaba.fastjson.JSONObject in project weex-example by KalicyZhou.
the class WXDomStatementTest method testMoveDom.
@Test
public void testMoveDom() throws Exception {
createBody();
JSONObject obj;
obj = new JSONObject();
obj.put("type", "div");
obj.put("ref", "100");
stmt.addDom(obj, WXDomObject.ROOT, 0);
obj = new JSONObject();
obj.put("type", "div");
obj.put("ref", "101");
stmt.addDom(obj, WXDomObject.ROOT, 0);
stmt.moveDom("100", WXDomObject.ROOT, 1);
stmt.batch();
}
use of com.alibaba.fastjson.JSONObject in project weex-example by KalicyZhou.
the class WXStreamModule method fetch.
/**
*
* @param optionsStr request options include:
* method: GET 、POST、PUT、DELETE、HEAD、PATCH
* headers:object,请求header
* url:
* body: "Any body that you want to add to your request"
* type: json、text、jsonp(native实现时等价与json)
* @param callback finished callback,response object:
* status:status code
* ok:boolean 是否成功,等价于status200~299
* statusText:状态消息,用于定位具体错误原因
* data: 响应数据,当请求option中type为json,时data为object,否则data为string类型
* headers: object 响应头
*
* @param progressCallback in progress callback,for download progress and request state,response object:
* readyState: number 请求状态,1 OPENED,开始连接;2 HEADERS_RECEIVED;3 LOADING
* status:status code
* length:当前获取的字节数,总长度从headers里「Content-Length」获取
* statusText:状态消息,用于定位具体错误原因
* headers: object 响应头
*/
@JSMethod(uiThread = false)
public void fetch(String optionsStr, final JSCallback callback, JSCallback progressCallback) {
JSONObject optionsObj = null;
try {
optionsObj = JSON.parseObject(optionsStr);
} catch (JSONException e) {
WXLogUtils.e("", e);
}
boolean invaildOption = optionsObj == null || optionsObj.getString("url") == null;
if (invaildOption) {
if (callback != null) {
Map<String, Object> resp = new HashMap<>();
resp.put("ok", false);
resp.put(STATUS_TEXT, Status.ERR_INVALID_REQUEST);
callback.invoke(resp);
}
return;
}
String method = optionsObj.getString("method");
String url = optionsObj.getString("url");
JSONObject headers = optionsObj.getJSONObject("headers");
String body = optionsObj.getString("body");
String type = optionsObj.getString("type");
int timeout = optionsObj.getIntValue("timeout");
if (method != null)
method = method.toUpperCase();
Options.Builder builder = new Options.Builder().setMethod(!"GET".equals(method) && !"POST".equals(method) && !"PUT".equals(method) && !"DELETE".equals(method) && !"HEAD".equals(method) && !"PATCH".equals(method) ? "GET" : method).setUrl(url).setBody(body).setType(type).setTimeout(timeout);
extractHeaders(headers, builder);
final Options options = builder.createOptions();
sendRequest(options, new ResponseCallback() {
@Override
public void onResponse(WXResponse response, Map<String, String> headers) {
if (callback != null) {
Map<String, Object> resp = new HashMap<>();
if (response == null || "-1".equals(response.statusCode)) {
resp.put(STATUS, -1);
resp.put(STATUS_TEXT, Status.ERR_CONNECT_FAILED);
} else {
int code = Integer.parseInt(response.statusCode);
resp.put(STATUS, code);
resp.put("ok", (code >= 200 && code <= 299));
if (response.originalData == null) {
resp.put("data", null);
} else {
String respData = readAsString(response.originalData, headers != null ? getHeader(headers, "Content-Type") : "");
try {
resp.put("data", parseData(respData, options.getType()));
} catch (JSONException exception) {
WXLogUtils.e("", exception);
resp.put("ok", false);
resp.put("data", "{'err':'Data parse failed!'}");
}
}
resp.put(STATUS_TEXT, Status.getStatusText(response.statusCode));
}
resp.put("headers", headers);
callback.invoke(resp);
}
}
}, progressCallback);
}
use of com.alibaba.fastjson.JSONObject in project weex-example by KalicyZhou.
the class WXStreamModule method sendHttp.
/**
* send HTTP request
*
* @param params {method:POST/GET/PUT/DELETE/HEAD/PATCH,url:http://xxx,header:{key:value},
* body:{key:value}}
* @param callback formate:handler(err, response)
*/
@Deprecated
@JSMethod(uiThread = false)
public void sendHttp(String params, final String callback) {
JSONObject paramsObj = JSON.parseObject(params);
String method = paramsObj.getString("method");
String url = paramsObj.getString("url");
JSONObject headers = paramsObj.getJSONObject("header");
String body = paramsObj.getString("body");
int timeout = paramsObj.getIntValue("timeout");
if (method != null)
method = method.toUpperCase();
Options.Builder builder = new Options.Builder().setMethod(!"GET".equals(method) && !"POST".equals(method) && !"PUT".equals(method) && !"DELETE".equals(method) && !"HEAD".equals(method) && !"PATCH".equals(method) ? "GET" : method).setUrl(url).setBody(body).setTimeout(timeout);
extractHeaders(headers, builder);
sendRequest(builder.createOptions(), new ResponseCallback() {
@Override
public void onResponse(WXResponse response, Map<String, String> headers) {
if (callback != null && mWXSDKInstance != null)
WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callback, (response == null || response.originalData == null) ? "{}" : readAsString(response.originalData, headers != null ? getHeader(headers, "Content-Type") : ""));
}
}, null);
}
use of com.alibaba.fastjson.JSONObject in project weex-example by KalicyZhou.
the class WXDomObject method parseFromJson.
/**
* Parse the jsonObject to {@link WXDomObject} recursively
* @param map the original JSONObject
*/
public void parseFromJson(JSONObject map) {
if (map == null || map.size() <= 0) {
return;
}
String type = (String) map.get("type");
this.mType = type;
this.mRef = (String) map.get("ref");
Object style = map.get("style");
if (style != null && style instanceof JSONObject) {
WXStyle styles = new WXStyle();
styles.putAll((JSONObject) style, false);
this.mStyles = styles;
}
Object attr = map.get("attr");
if (attr != null && attr instanceof JSONObject) {
WXAttr attrs = new WXAttr((JSONObject) attr);
//WXJsonUtils.putAll(attrs, (JSONObject) attr);
this.mAttributes = attrs;
}
Object event = map.get("event");
if (event != null && event instanceof JSONArray) {
WXEvent events = new WXEvent();
JSONArray eventArray = (JSONArray) event;
int count = eventArray.size();
for (int i = 0; i < count; ++i) {
events.add(eventArray.getString(i));
}
this.mEvents = events;
}
}
Aggregations