use of org.yh.library.okhttp.callback.StringCallback in project YhLibraryForAndroid by android-coco.
the class YHOkHttp1 method postForm.
private static void postForm(final String host, final String suffix, final Map<String, Object> params, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut, final int control) {
String prefix = "";
Map<String, String> mapUrls = null;
if (!isEmpty(host) && !host.equals(Constants.HOST_MAIN)) {
// mapUrls = getHost();
prefix = mapUrls.get(host);
}
if (host.equals(Constants.HOST_MAIN)) {
prefix = Constants.HOST_MAIN;
}
String url = "";
url = prefix + suffix;
LogUtils.e("postForm请求host:", url + " 第几次请求:" + control);
final String url1 = url;
// 组成的URL 为空或者不是http或https开头都是非法URL
if (isEmpty(url1) || (!url1.startsWith(Constants.FILE_HTTP) && !url1.startsWith(Constants.FILE_HTTPS))) {
callback.onFailure(-3, "非法URL");
callback.onFinish();
return;
}
PostFormBuilder formbuilder = OkHttpUtils.post();
formbuilder.url(url1).tag(tag);
Iterator<Entry<String, Object>> paramsIterator = params.entrySet().iterator();
while (paramsIterator.hasNext()) {
Entry<String, Object> entry = paramsIterator.next();
// key
String key = entry.getKey();
// 值
Object obj = entry.getValue();
if (obj instanceof File) {
// 如果是文件
File newFile = (File) obj;
formbuilder.addFile(key, newFile.getName(), newFile);
} else if (obj instanceof String) {
// 如果是字符
formbuilder.addParams(key, (String) obj);
}
LogUtils.e("postForm参数集:", "key:" + key + " Value:" + obj);
}
formbuilder.build().connTimeOut(connTimeOut).readTimeOut(readTimeOut).writeTimeOut(//
writeTimeOut).execute(new StringCallback() {
@Override
public void onResponse(String response, int id) {
try {
JSONObject json = new JSONObject(response);
String ret = json.getString(Constants.RESULT);
if (ERROE_3001.equals(ret) || ERROE_3002.equals(ret)) {
// 相关操作
// EventBus.getDefault().post(
// new EventBusBean(new Intent(
// AppConfig.ACTION_PLOGIN_OUT)));
// EventBus.getDefault().post(
// new EventBusBean(new Intent(
// AppConfig.ACTION_AGAIN_TOKEN)));
} else {
callback.onSuccess(response);
callback.onFinish();
}
} catch (JSONException e) {
callback.onSuccess(response);
callback.onFinish();
}
}
@Override
public void onError(Call call, Exception e, int id) {
LogUtils.e("OkHttpRequestManager", "postForm请求URL:" + url1 + " 请求错误:" + e.getMessage() + " " + id);
dealError(e, callback);
}
@Override
public void onAfter(int id) {
super.onAfter(id);
}
});
}
use of org.yh.library.okhttp.callback.StringCallback in project YhLibraryForAndroid by android-coco.
the class YHOkHttp1 method get.
private static void get(final String host, final String suffix, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut, final int control) {
String prefix = "";
Map<String, String> mapUrls = null;
if (!isEmpty(host) && !host.equals(Constants.HOST_MAIN)) {
// mapUrls = getHost();
prefix = mapUrls.get(host);
}
if (host.equals(Constants.HOST_MAIN)) {
prefix = Constants.HOST_MAIN;
}
String url = "";
url = prefix + suffix;
LogUtils.e("GET请求host:", url + " ");
final String url1 = url;
// 组成的URL 为空或者不是http或https开头都是非法URL
if (isEmpty(url1) || (!url1.startsWith(Constants.FILE_HTTP) && !url1.startsWith(Constants.FILE_HTTPS))) {
callback.onFailure(-3, "非法URL");
callback.onFinish();
return;
}
OkHttpUtils.get().url(url1).tag(tag).build().connTimeOut(connTimeOut).readTimeOut(readTimeOut).writeTimeOut(writeTimeOut).execute(new StringCallback() {
@Override
public void onResponse(String response, int id) {
try {
JSONObject json = new JSONObject(response);
String ret = json.getString(Constants.RESULT);
if (ERROE_3001.equals(ret) || ERROE_3002.equals(ret)) {
// 相关操作
// EventBus.getDefault().post(
// new EventBusBean(new Intent(
// AppConfig.ACTION_PLOGIN_OUT)));
// EventBus.getDefault().post(
// new EventBusBean(new Intent(
// AppConfig.ACTION_AGAIN_TOKEN)));
} else {
callback.onSuccess(response);
callback.onFinish();
}
} catch (JSONException e) {
callback.onSuccess(response);
callback.onFinish();
}
}
@Override
public void onError(Call call, Exception e, int id) {
LogUtils.e("OkHttpRequestManager", "GET请求URL:" + url1 + " 请求错误:" + e.getMessage() + " " + id);
String error = e.getMessage();
if (!isEmpty(error)) {
if (error.startsWith("failed to connect to")) {
callback.onFailure(-4, "连接超时");
callback.onFinish();
return;
}
}
if (control == 1) {
get(host, suffix, callback, tag, connTimeOut, readTimeOut, writeTimeOut, control + 1);
} else if (control == 2) {
if (isEmpty(host) || host.equals(Constants.HOST_MAIN)) {
dealError(e, callback);
} else {
requestHostConfiguration(tag, new StringCallback() {
@Override
public void onResponse(String response, int id) {
boolean x = jxHOST(response);
if (x) {
get(host, suffix, callback, tag, connTimeOut, readTimeOut, writeTimeOut, control + 1);
} else {
callback.onFailure(-2, "JSON解析异常");
callback.onFinish();
}
}
@Override
public void onError(Call call, Exception e, int id) {
dealError(e, callback);
}
});
}
} else if (control == 3) {
dealError(e, callback);
}
}
});
}
Aggregations