NetHelper.get("url", new NetSingleBeanListener() {
@Override
protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
// 这里是ui线程
}
@Override
protected void onSuccess(NetUserBean userBean) {
// 这里是ui线程
}
});
private void startRequest() {
new Thread() {
@Override
public void run() {
request1();
request2();
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
Can't create handler inside thread that has not called Looper.prepare()
private void startRequest() {
new Thread() {
@Override
public void run() {
NetHelper.get("url1", new NetStringListener() {
@Override
protected void onSuccess(String string) {
}
@Override
protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
}
}
NetHelper.get("url2", new NetStringListener() {
@Override
protected void onSuccess(String string) {
}
@Override
protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
}
}
}.start();
}
private void startRequest() {
new Thread() {
@Override
public void run() {
if (request1()) {
request2();
}
}
}.start();
}
private void startRequest() {
new Thread() {
@Override
public void run() {
NetHelper.get("url1", new NetStringListener() {
@Override
protected void onSuccess(String string) {
NetHelper.get("url2", new NetStringListener() {
@Override
protected void onSuccess(String string) {
}
@Override
protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
}
});
}
@Override
protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
}
});
}
}.start();
}
private void startRequest() {
new Thread() {
@Override
public void run() {
boolean ret = NetHelper.get("url1", new NetStringListener() {
@Override
protected void onSuccess(String string) {
}
@Override
protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
}
});
if (ret) {
NetHelper.get("url2", new NetStringListener() {
@Override
protected void onSuccess(String string) {
}
@Override
protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
}
});
}
}
}.start();
}
private void startRequest() {
new Thread() {
@Override
public void run() {
boolean ret = NetHelper.get("url1", new new NetTempListener());
if (ret) {
NetHelper.get("url2", new NetTempListener());
}
}
}.start();
}
private void startRequest() {
new Thread() {
@Override
public void run() {
NetRetBean netRetBean = NetHelper.getSync("url", new NetTempListener());
CallbackCode callbackCode = netRetBean.getCallbackCode();
switch (callbackCode) {
case CODE_SUCCESS_REQUEST:
String string = (String) netRetBean.getServerObject();
System.out.println(string);
break;
case CODE_ERROR_HTTP_NOT_200:
case CODE_ERROR_REQUEST_EXP:
case CODE_ERROR_SERVER_DATA_ERROR:
case CODE_ERROR_JSON_EXP:
case CODE_ERROR_UNKNOWN:
default:
System.out.println(netRetBean.toString());
break;
}
}
}.start();
}
private void startRequest() {
new Thread() {
@Override
public void run() {
NetRetBean netRetBean = NetHelper.getSync("url1", new NetTempListener());
if (netRetBean.isSuccess()) {
NetHelper.getSync("url2", new NetTempListener());
}
}
}.start();
}
public class NetHelper {
public static NetRetBean getSync(String url, boolean isWaitForToken, NetListener netListener) {
NetExcutor netExcutor = new NetExcutor();
netExcutor.setUrl(url);
netExcutor.setWaitForToken(isWaitForToken);
netExcutor.setNetListener(netListener);
return netExcutor.get();
}
}
package com.chenjian.net.core.sync;
import com.chenjian.net.bean.NetRetBean;
import com.chenjian.net.core.common.RequestType;
import com.chenjian.net.request.RequestUtil;
import com.chenjian.net.token.TokenUtil;
/**
* 网络请求处理核心类
*
* 作者: ChenJian
* 时间: 2016.12.13 17:42
*/
public class SyncNetExcutor {
/**
* 请求url
*/
private String mUrl;
/**
* 请求类型
*/
private RequestType mRequestType;
/**
* post请求时的参数
*/
private String mParams;
/**
* 是否先等待token请求成功
*/
private boolean isWaitForToken;
/**
* 请求后的回调
*/
private SyncNetListener mSyncNetListener;
public void setUrl(String url) {
this.mUrl = url;
}
private void setRequestType(RequestType requestType) {
this.mRequestType = requestType;
}
public void setParams(String params) {
this.mParams = params;
}
public void setWaitForToken(boolean waitForToken) {
isWaitForToken = waitForToken;
}
public void setSyncNetListener(SyncNetListener syncNetListener) {
this.mSyncNetListener = syncNetListener;
}
public NetRetBean get() {
setRequestType(RequestType.REQUEST_TYPE_GET);
return startRequest();
}
public NetRetBean post() {
setRequestType(RequestType.REQUEST_TYPE_POST);
return startRequest();
}
/**
* 同步请求,直接返回
*
* @return 返回 NetRetBean
*/
private NetRetBean startRequest() {
if (isWaitForToken) {
TokenUtil.waitToken();
}
try {
String result = request();
return mSyncNetListener.sendSuccess(result);
} catch (Exception e) {
e.printStackTrace();
return mSyncNetListener.sendError(e);
}
}
public String getString() {
setRequestType(RequestType.REQUEST_TYPE_GET);
return startRequestString();
}
public String postString() {
setRequestType(RequestType.REQUEST_TYPE_POST);
return startRequestString();
}
/**
* 同步请求,直接返回
*
* @return 返回 String
*/
private String startRequestString() {
if (isWaitForToken) {
TokenUtil.waitToken();
}
try {
return request();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private String request() throws Exception {
String result = null;
switch (mRequestType) {
case REQUEST_TYPE_GET:
result = RequestUtil.getRequest(mUrl);
break;
case REQUEST_TYPE_POST:
result = RequestUtil.postRequest(mUrl, mParams);
break;
default:
break;
}
return result;
}
}
package com.chenjian.net.core.sync;
import com.chenjian.net.bean.NetRetBean;
/**
* 网络请求回调核心类
*
* 作者: ChenJian
* 时间: 2016.12.13 17:42
*/
public interface SyncNetListener {
/**
* http请求,数据解密部分,成功
*
* @param result result
* @return NetRetBean
*/
NetRetBean sendSuccess(String result);
/**
* http请求,数据解密部分,失败
*
* @param e e
* @return NetRetBean
*/
NetRetBean sendError(Exception e);
}
package com.chenjian.net.helper;
import com.chenjian.net.bean.NetRetBean;
import com.chenjian.net.core.async.NetExcutor;
import com.chenjian.net.core.async.NetListener;
import com.chenjian.net.core.sync.SyncNetExcutor;
import com.chenjian.net.core.sync.SyncNetListener;
import com.chenjian.net.data.NetConstants;
/**
* 网络请求工具类
*
* 作者: ChenJian
* 时间: 2016.12.14 11:24
*/
public class NetHelper {
/**
* get同步请求
*
* @param url url
* @param syncNetListener 监听器
* @return NetRetBean
*/
public static NetRetBean getSync(String url, SyncNetListener syncNetListener) {
return getSync(url, NetConstants.defaultWaitForToken, syncNetListener);
}
/**
* get同步请求
*
* @param url url
* @param isWaitForToken 是否等待token请求成功
* @param syncNetListener 监听器
* @return NetRetBean
*/
public static NetRetBean getSync(String url, boolean isWaitForToken, SyncNetListener syncNetListener) {
SyncNetExcutor syncNetExcutor = new SyncNetExcutor();
syncNetExcutor.setUrl(url);
syncNetExcutor.setWaitForToken(isWaitForToken);
syncNetExcutor.setSyncNetListener(syncNetListener);
return syncNetExcutor.get();
}
/**
* post同步请求
*
* @param url url
* @param params 参数
* @param syncNetListener 监听器
* @return NetRetBean
*/
public static NetRetBean postSync(String url, String params, SyncNetListener syncNetListener) {
return postSync(url, params, NetConstants.defaultWaitForToken, syncNetListener);
}
/**
* post同步请求
*
* @param url url
* @param params 参数
* @param isWaitForToken 是否等待token请求成功
* @param syncNetListener 监听器
* @return NetRetBean
*/
public static NetRetBean postSync(String url, String params, boolean isWaitForToken, SyncNetListener syncNetListener) {
SyncNetExcutor syncNetExcutor = new SyncNetExcutor();
syncNetExcutor.setUrl(url);
syncNetExcutor.setParams(params);
syncNetExcutor.setWaitForToken(isWaitForToken);
syncNetExcutor.setSyncNetListener(syncNetListener);
return syncNetExcutor.post();
}
/**
* get同步请求,不设置监听器,直接返回数据给调用者
*
* @param url url
* @return String
*/
public static String getStringSync(String url) {
return getStringSync(url, NetConstants.defaultWaitForToken);
}
/**
* get同步请求,不设置监听器,直接返回数据给调用者
*
* @param url url
* @param isWaitForToken 是否等待token请求成功
* @return String
*/
public static String getStringSync(String url, boolean isWaitForToken) {
SyncNetExcutor syncNetExcutor = new SyncNetExcutor();
syncNetExcutor.setUrl(url);
syncNetExcutor.setWaitForToken(isWaitForToken);
return syncNetExcutor.getString();
}
/**
* post同步请求,不设置监听器,直接返回数据给调用者
*
* @param url url
* @param params 参数
* @return String
*/
public static String postStringSync(String url, String params) {
return postStringSync(url, params, NetConstants.defaultWaitForToken);
}
/**
* post同步请求,不设置监听器,直接返回数据给调用者
*
* @param url url
* @param params 参数
* @param isWaitForToken 是否等待token请求成功
* @return String
*/
public static String postStringSync(String url, String params, boolean isWaitForToken) {
SyncNetExcutor syncNetExcutor = new SyncNetExcutor();
syncNetExcutor.setUrl(url);
syncNetExcutor.setParams(params);
syncNetExcutor.setWaitForToken(isWaitForToken);
return syncNetExcutor.postString();
}
}
private void syncGetString() {
new Thread() {
@Override
public void run() {
String getString = NetHelper.getStringSync("url");
System.out.println(getString);
String postString = NetHelper.getStringSync("url");
System.out.println(postString);
}
}.start();
}
private void startRequest() {
new Thread() {
@Override
public void run() {
NetRetBean netRetBean = NetHelper.getSync("url", new SyncNetListener() {
@Override
public NetRetBean sendSuccess(String result) {
return null;
}
@Override
public NetRetBean sendError(Exception e) {
return null;
}
});
}
}.start();
}
public NetRetBean get() {
setRequestType(RequestType.REQUEST_TYPE_GET);
return startRequest();
}
public NetRetBean post() {
setRequestType(RequestType.REQUEST_TYPE_POST);
return startRequest();
}
/**
* 同步请求,直接返回
*
* @return 返回 NetRetBean
*/
private NetRetBean startRequest() {
if (isWaitForToken) {
TokenUtil.waitToken();
}
try {
String result = request();
return mSyncNetListener.sendSuccess(result);
} catch (Exception e) {
e.printStackTrace();
return mSyncNetListener.sendError(e);
}
}
package com.chenjian.net.listener.sync;
import com.chenjian.net.bean.NetRetBean;
import com.chenjian.net.core.sync.SyncNetListener;
import com.chenjian.net.exp.RequestErrorException;
import com.chenjian.net.exp.RespondErrorException;
import com.chenjian.net.listener.common.CallbackCode;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 公用网络逻辑,核心监听器。自定义监听器一般继承这个类
*
* 作者: ChenJian
* 时间: 2016.12.15 11:12
*/
abstract public class SyncNetHandleListener implements SyncNetListener {
/**
* 处理NetRetBean并返回。是一个中转站。本类和其子类都可以调用这个方法
*
* @param netRetBean netRetBean
* @return netRetBean
*/
protected NetRetBean handleResult(NetRetBean netRetBean) {
return netRetBean;
}
@Override
public NetRetBean sendSuccess(String result) {
NetRetBean netRetBean = new NetRetBean();
try {
JSONObject jsonObject = new JSONObject(result);
String code = jsonObject.getString("code");
String message = jsonObject.getString("message");
String time = jsonObject.getString("time");
String data = jsonObject.getString("data");
netRetBean.setServerCode(code);
netRetBean.setServerMsg(message);
netRetBean.setServerTime(time);
netRetBean.setServerData(data);
if (code.equals("00001")) {
netRetBean.setCallbackCode(CallbackCode.CODE_SUCCESS_REQUEST);
netRetBean = onReceivedRet(netRetBean);
} else {
netRetBean.setCallbackCode(CallbackCode.CODE_ERROR_SERVER_DATA_ERROR);
}
} catch (JSONException e) {
e.printStackTrace();
netRetBean.setCallbackCode(CallbackCode.CODE_ERROR_JSON_EXP);
}
return handleResult(netRetBean);
}
@Override
public NetRetBean sendError(Exception exp) {
exp.printStackTrace();
NetRetBean netRetBean = new NetRetBean();
netRetBean.setException(exp);
try {
throw exp;
} catch (RespondErrorException e) {
netRetBean.setCallbackCode(CallbackCode.CODE_ERROR_HTTP_NOT_200);
} catch (RequestErrorException e) {
netRetBean.setCallbackCode(CallbackCode.CODE_ERROR_REQUEST_EXP);
} catch (JSONException e) {
netRetBean.setCallbackCode(CallbackCode.CODE_ERROR_JSON_EXP);
} catch (Exception e) {
netRetBean.setCallbackCode(CallbackCode.CODE_ERROR_UNKNOWN);
}
return handleResult(netRetBean);
}
/**
* 子类根据业务区分,将netRetBean解析成list或者单个实体,或者解析成其它结果
*
* @param netRetBean server返回的数据实体,data字段将在子类中解析
* @return 解析后的netRetBean
* @throws JSONException 解析json异常
*/
abstract protected NetRetBean onReceivedRet(NetRetBean netRetBean) throws JSONException;
}
private void syncGetString() {
new Thread() {
@Override
public void run() {
NetRetBean netRetBean = NetHelper.getSync("url", new SyncNetHandleListener() {
@Override
protected NetRetBean onReceivedRet(NetRetBean netRetBean) throws JSONException {
return null;
}
});
}
}.start();
}