use of org.yh.library.okhttp.callback.StringCallback in project YhLibraryForAndroid by android-coco.
the class OkHttpRequestManager method postForm.
public void postForm(final String host, final String suffix, Map<String, String> headers, final Map<String, Object> params, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut) {
final String url = host + suffix;
// 组成的URL 为空或者不是http或https开头都是非法URL
if (isEmpty(url) || (!url.startsWith(Constants.FILE_HTTP) && !url.startsWith(Constants.FILE_HTTPS))) {
callback.onFailure(-3, "非法URL");
callback.onFinish();
return;
}
if (StringUtils.isEmpty(headers)) {
headers = this.headers;
}
PostFormBuilder formbuilder = OkHttpUtils.post();
formbuilder.url(url).tag(tag).headers(headers);
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:" + url
// + " 请求错误:" + 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 OkHttpRequestManager method post.
@Override
public void post(final String host, final String suffix, Map<String, String> headers, final Map<String, String> params, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut) {
final String url = host + suffix;
// 组成的URL 为空或者不是http或https开头都是非法URL
if (isEmpty(url) || (!url.startsWith(Constants.FILE_HTTP) && !url.startsWith(Constants.FILE_HTTPS))) {
callback.onFailure(-3, "非法URL");
callback.onFinish();
return;
}
if (StringUtils.isEmpty(headers)) {
headers = this.headers;
}
OkHttpUtils.post().url(url).headers(headers).tag(tag).params(params).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) {
// + e + " " + id);
if (!isEmpty(e)) {
if (e instanceof java.net.SocketTimeoutException) {
callback.onFailure(-5, "请求超时");
callback.onFinish();
} else if (e instanceof java.net.ConnectException) {
callback.onFailure(-4, "连接超时");
callback.onFinish();
} else {
dealError(e, callback);
}
}
}
});
}
use of org.yh.library.okhttp.callback.StringCallback in project YhLibraryForAndroid by android-coco.
the class OkHttpRequestManager method postString.
@Override
public void postString(final String host, final String suffix, Map<String, String> headers, final String params, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut) {
final String url = host + suffix;
// 组成的URL 为空或者不是http或https开头都是非法URL
if (isEmpty(url) || (!url.startsWith(Constants.FILE_HTTP) && !url.startsWith(Constants.FILE_HTTPS))) {
callback.onFailure(-3, "非法URL");
callback.onFinish();
return;
}
if (StringUtils.isEmpty(headers)) {
headers = this.headers;
}
OkHttpUtils.postString().url(url).headers(headers).tag(tag).content(params).mediaType(MediaType.parse("application/json; charset=utf-8")).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) {
// + e + " " + id);
if (!isEmpty(e)) {
if (e instanceof java.net.SocketTimeoutException) {
callback.onFailure(-5, "请求超时");
callback.onFinish();
} else if (e instanceof java.net.ConnectException) {
callback.onFailure(-4, "连接超时");
callback.onFinish();
} else {
dealError(e, callback);
}
}
}
});
}
use of org.yh.library.okhttp.callback.StringCallback in project YhLibraryForAndroid by android-coco.
the class YHOkHttp1 method post.
private static void post(final String host, final String suffix, final Map<String, String> 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("post请求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;
}
OkHttpUtils.post().url(url1).tag(tag).params(params).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) {
// failed to connect to
LogUtils.e("OkHttpRequestManager", "POST请求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) {
post(host, suffix, params, 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) {
post(host, suffix, params, 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);
}
}
});
}
use of org.yh.library.okhttp.callback.StringCallback in project YhLibraryForAndroid by android-coco.
the class OkHttpRequestManager method get.
@Override
public void get(final String host, final String suffix, Map<String, String> headers, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut) {
String url = host + 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;
}
if (StringUtils.isEmpty(headers)) {
headers = this.headers;
}
OkHttpUtils.get().url(url1).headers(headers).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) {
// + e.getMessage() + " " + id);
if (!isEmpty(e)) {
if (e instanceof java.net.SocketTimeoutException) {
callback.onFailure(-5, "请求超时");
callback.onFinish();
} else if (e instanceof java.net.ConnectException) {
callback.onFailure(-4, "连接超时");
callback.onFinish();
} else {
dealError(e, callback);
}
}
}
});
}
Aggregations