use of com.taobao.weex.common.WXRequest in project WeexErosFramework by bmfe.
the class AxiosManager method loadJSBundle.
public void loadJSBundle(final WXRequest request, final DefaultWXHttpAdapter.JSBundleCallback listener) {
final WXResponse wxResponse = new WXResponse();
String method = request.method == null ? "GET" : request.method.toUpperCase();
String requestBodyString = request.body == null ? "{}" : request.body;
RequestBody body = null;
if (request.paramMap != null && request.paramMap.containsKey("Content-Type")) {
body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse(request.paramMap.get("Content-Type")), requestBodyString) : null;
} else {
body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;" + "charset=UTF-8"), requestBodyString) : null;
}
Request.Builder requestBuilder = new Request.Builder().url(request.url).method(method, body);
if (request.paramMap != null) {
for (Map.Entry<String, String> param : request.paramMap.entrySet()) {
requestBuilder.addHeader(param.getKey(), TextUtil.toHumanReadableAscii(param.getValue()));
}
}
OkHttpUtils.getInstance().getOkHttpClient().newCall(requestBuilder.build()).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
wxResponse.errorMsg = e.getMessage();
wxResponse.errorCode = "-1";
wxResponse.statusCode = "-1";
if (listener != null) {
listener.onFailure(wxResponse);
}
}
@Override
public void onResponse(Call call, Response response) {
byte[] responseBody = new byte[0];
try {
responseBody = response.body().bytes();
} catch (IOException e) {
e.printStackTrace();
wxResponse.errorMsg = e.getMessage();
wxResponse.errorCode = "-1";
wxResponse.statusCode = "-1";
if (listener != null) {
listener.onFailure(wxResponse);
}
}
wxResponse.data = new String(responseBody);
wxResponse.statusCode = String.valueOf(response.code());
wxResponse.originalData = responseBody;
wxResponse.extendParams = new HashMap<>();
for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
wxResponse.extendParams.put(entry.getKey(), entry.getValue());
}
if (response.code() < 200 || response.code() > 299) {
wxResponse.errorMsg = response.message();
if (listener != null) {
listener.onFailure(wxResponse);
}
} else {
if (listener != null) {
listener.onResponse(wxResponse);
}
}
}
});
}
use of com.taobao.weex.common.WXRequest in project WeexErosFramework by bmfe.
the class TypeFaceHandler method fetchIcon.
private static void fetchIcon(String url, IWXHttpAdapter.OnHttpListener listener) {
IWXHttpAdapter adapter = WXSDKManager.getInstance().getIWXHttpAdapter();
if (adapter == null) {
WXLogUtils.e(TAG, "downloadFontByNetwork() IWXHttpAdapter == null");
return;
}
WXRequest request = new WXRequest();
request.url = url;
request.method = "GET";
adapter.sendRequest(request, listener);
}
use of com.taobao.weex.common.WXRequest in project WeexErosFramework by bmfe.
the class TypeFaceHandler method load.
public static void load(String url, IWXHttpAdapter.OnHttpListener listener) {
Uri parse = Uri.parse(url);
listener.onHttpStart();
DefaultTypefaceAdapter typefaceAdapter = BMWXApplication.getWXApplication().getTypefaceAdapter();
if (typefaceAdapter == null) {
Log.e(TAG, "未找到支持bmLocal的adapter");
return;
}
if ("bmlocal".equalsIgnoreCase(parse.getScheme())) {
// bmlocal
if (typefaceAdapter.isInterceptor()) {
// load local
File iconDir = typefaceAdapter.getTypefaceDir();
if (!iconDir.exists())
return;
File localIcon = new File(iconDir, parse.getPath());
if (!localIcon.exists()) {
// local icon not exists
WXResponse response = new WXResponse();
response.statusCode = "404";
if (listener != null) {
listener.onHttpFinish(response);
}
return;
}
loadLocalIncon(localIcon, listener);
} else {
// fetch on Js server
String iconDownloadUrl = typefaceAdapter.getJsServer() + "/dist";
if (TextUtils.isEmpty(iconDownloadUrl))
return;
String fetchUrl = iconDownloadUrl + "/" + parse.getHost() + parse.getPath();
fetchIcon(fetchUrl, listener);
}
} else if ("http".equalsIgnoreCase(parse.getScheme()) || "https".equalsIgnoreCase(parse.getScheme())) {
// http
WXRequest wxRequest = new WXRequest();
wxRequest.url = url;
wxRequest.method = "GET";
requestRemoteIcon(wxRequest, listener);
}
}
use of com.taobao.weex.common.WXRequest in project weex-example by KalicyZhou.
the class WXStreamModuleTest method testFetchSuccessFinish.
@Test
public void testFetchSuccessFinish() throws Exception {
IWXHttpAdapter adapter = new IWXHttpAdapter() {
@Override
public void sendRequest(WXRequest request, OnHttpListener listener) {
listener.onHttpFinish(successResponse());
}
};
WXStreamModule streamModule = createModule(adapter);
Callback cb = new Callback();
streamModule.fetch("{'url':'http://www.taobao.com'}", cb, null);
assert (boolean) cb.mData.get("ok");
}
use of com.taobao.weex.common.WXRequest in project weex-example by KalicyZhou.
the class WXStreamModuleTest method testFetchHeaderReceived.
@Test
public void testFetchHeaderReceived() throws Exception {
IWXHttpAdapter adapter = new IWXHttpAdapter() {
@Override
public void sendRequest(WXRequest request, OnHttpListener listener) {
Map<String, List<String>> headers = new HashMap<>();
headers.put("key", Arrays.asList("someval"));
listener.onHeadersReceived(200, headers);
}
};
WXStreamModule streamModule = createModule(adapter);
Callback cb = new Callback();
streamModule.fetch("{'url':'http://www.taobao.com'}", null, cb);
assert ((Map<String, String>) cb.mData.get("headers")).get("key").equals("someval");
}
Aggregations