use of com.taobao.weex.common.WXResponse in project incubator-weex by apache.
the class WXStreamModule method fetch.
/**
* @param optionsStr request options include:
* method: GET 、POST、PUT、DELETE、HEAD、PATCH
* headers:object,request header
* url:
* body: "Any body that you want to add to your request"
* type: json、text、jsonp(json)
* @param callback finished callback,response object:
* status:status code
* ok:boolean is success,http status200~299
* statusText: statusText
* data: option type is json,data is object,not data is string
* headers: headers
*
* @param progressCallback in progress callback,for download progress and request state,response object:
* readyState: number connection status 1 OPENED 2 HEADERS_RECEIVED 3 LOADING
* status:status code
* length:headers Content-Length
* statusText:statusText
* headers: headers
*/
@JSMethod(uiThread = false)
public void fetch(JSONObject optionsObj, final JSCallback callback, JSCallback progressCallback) {
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.taobao.weex.common.WXResponse in project incubator-weex by apache.
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(JSONObject paramsObj, final String callback) {
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.taobao.weex.common.WXResponse in project incubator-weex by apache.
the class WXStreamModuleTest method successResponse.
private WXResponse successResponse() {
WXResponse resp = new WXResponse();
resp.data = "data";
resp.statusCode = "200";
return resp;
}
use of com.taobao.weex.common.WXResponse in project incubator-weex by apache.
the class DefaultWXHttpAdapter method sendRequest.
@Override
public void sendRequest(final WXRequest request, final OnHttpListener listener) {
if (listener != null) {
listener.onHttpStart();
}
execute(new Runnable() {
@Override
public void run() {
WXResponse response = new WXResponse();
IEventReporterDelegate reporter = getEventReporterDelegate();
try {
HttpURLConnection connection = openConnection(request, listener);
reporter.preConnect(connection, request.body);
Map<String, List<String>> headers = connection.getHeaderFields();
int responseCode = connection.getResponseCode();
if (listener != null) {
listener.onHeadersReceived(responseCode, headers);
}
reporter.postConnect();
response.statusCode = String.valueOf(responseCode);
if (responseCode >= 200 && responseCode <= 299) {
InputStream rawStream = connection.getInputStream();
rawStream = reporter.interpretResponseStream(rawStream);
response.originalData = readInputStreamAsBytes(rawStream, listener);
} else {
response.errorMsg = readInputStream(connection.getErrorStream(), listener);
}
if (listener != null) {
listener.onHttpFinish(response);
}
} catch (IOException | IllegalArgumentException e) {
e.printStackTrace();
response.statusCode = "-1";
response.errorCode = "-1";
response.errorMsg = e.getMessage();
if (listener != null) {
listener.onHttpFinish(response);
}
if (e instanceof IOException) {
try {
reporter.httpExchangeFailed((IOException) e);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
});
}
use of com.taobao.weex.common.WXResponse in project JustAndroid by chinaltz.
the class OKHttpAdapter method sendRequest.
@Override
public void sendRequest(WXRequest request, final OnHttpListener listener) {
PostFormBuilder postFormBuilder = null;
GetBuilder getBuilder = null;
if ("POST".equals(request.method) || "PUT".equals(request.method) || "PATCH".equals(request.method)) {
postFormBuilder = OkHttpUtils.post().url(request.url);
} else {
getBuilder = OkHttpUtils.get().url(request.url);
}
if (request.paramMap != null) {
Set<String> keySets = request.paramMap.keySet();
for (String key : keySets) {
if (postFormBuilder != null) {
postFormBuilder.addParams(key, request.paramMap.get(key));
}
if (getBuilder != null) {
getBuilder.addParams(key, request.paramMap.get(key));
}
}
}
if (getBuilder != null) {
getBuilder.build().execute(new Callback() {
@Override
public Object parseNetworkResponse(Response response, int id) throws Exception {
WXResponse wxResponse = new WXResponse();
wxResponse.statusCode = String.valueOf(response.code());
wxResponse.data = response.body().string();
if (listener != null) {
listener.onHttpFinish(wxResponse);
}
return null;
}
@Override
public void onError(Call call, Exception e, int id) {
Log.d("mataa", e.getLocalizedMessage());
}
@Override
public void onResponse(Object response, int id) {
}
});
}
}
Aggregations