结构说明:
DoFunction(功能通信调用方法)
传入参数:postVo(Object)
成功传出:resultVo(Object)
失败传出:error(String)
作用:处理postVo,传入postVo,传出resultVo
铺助方法:
prepareData(Object postVo)//准备传入对象
hanldeData(Object resultVo)//处理传出对象
handleError(String error)//处理错误信息
encode(Object postVo)//加密方法
decode(Object resultVo)//解密方法
FunConnection(功能通信类)
固有参数:Action(String),HttpMethod(String)
传入参数:postVo(Object)
成功传出:resultVo(Object)
失败传出:error(String)
作用:传入postVo,处理jsonResult(String),传出resultVo
铺助方法:
successCallback
onSuccess(Object resultVo)//通信成功回调
failCallback
onFail(String error)//通信失败回调
NetConnection(通信基类)
固有参数:Server_url
传入参数:postVo(Object),Action(String),HttpMethod(String)
成功传出:resultJson(String)
失败传出:NUll
作用:解析postVo为jsonPost(String),组装url,发送jsonPost,传出jsonResult
铺助方法:
successCallback
onSuccess(String jsonPost)//通信成功回调
failCallback
onFail()//通信失败回调
源码示例:
doLogin(功能通信调用方法)
/**
* @throws Exception
*
* @Title: doLogin Description:用户登录通信 void
* @throws
*/
private void doLogin() throws Exception {
LoginPost loginPost = new LoginPost();
prepareData(loginPost);
try {
new Login(loginPost, new Login.SuccessCallback() {
@Override
public void onSuccess(LoginResult loginResult) {
handleData(loginResult);
}
}, new Login.FailCallback() {
@Override
public void onFail(String error) {
handleError(error);
}
});
} catch (Exception e) {
e.printStackTrace();
ToastUtil.showError(this, "通信失败");
}
}
Login.java(功能通信类)
package com.hxdb.wkj.net.login;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.hxdb.wkj.app.Config;
import com.hxdb.wkj.net.HttpMethod;
import com.hxdb.wkj.net.NetConnection;
import edu.xmut.ecom.app.sm.vo.login.LoginPost;
import edu.xmut.ecom.app.sm.vo.login.LoginResult;
import edu.xmut.ecom.app.sm.vo.result.BadResult;
/**
* Description: 用户登录网络类
*
* @author zhangxiaopan
* @date 2014-11-1
* @version 1.0
*
*/
public class Login {
public Login(LoginPost loginPost, final SuccessCallback successCallback,
final FailCallback failCallback) {
new NetConnection(Config.ACTION_LOGIN, loginPost, HttpMethod.POST,
new NetConnection.SuccessCallback() {
@Override
public void onSuccess(String result) {
try {
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject(result);
switch (jsonObject.getInt(Config.KEY_STATUS)) {
case Config.RESULT_STATUS_SUCCESS:
LoginResult loginResult = gson.fromJson(result,
LoginResult.class);
successCallback.onSuccess(loginResult);
break;
default:
BadResult badResult = gson.fromJson(result,
BadResult.class);
if (failCallback != null) {
failCallback.onFail(badResult.getError());
}
break;
}
} catch (JSONException e) {
e.printStackTrace();
if (failCallback != null) {
String error = "Gson解析错误";
failCallback.onFail(error);
}
}
}
}, new NetConnection.FailCallback() {
@Override
public void onFail() {
if (failCallback != null) {
String error = "通信异常";
failCallback.onFail(error);
}
}
});
}
public static interface SuccessCallback {
void onSuccess(LoginResult loginResult);
}
public static interface FailCallback {
void onFail(String error);
}
}
NetConnection.java(通信基类)
package com.hxdb.wkj.net;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import android.os.AsyncTask;
import android.util.Log;
import com.google.gson.Gson;
import com.hxdb.wkj.app.Config;
/**
*
* Description: 网络通信基类
*
* @author ouzhongyuan
* @date 2014-10-28
* @version 1.0
*
*/
public class NetConnection {
/**
*
* @Title: NetConnection Description:如何描述该构造方法
*
* @param action
* @param object
* @param method
* @param successCallback
* @param failCallback
*/
public NetConnection(final String action, final Object object,
final HttpMethod method, final SuccessCallback successCallback,
final FailCallback failCallback) {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
String url = Config.SERVER_URL + action + Config.SERVER_SUFFIX;// 组装Url
Gson gson = new Gson();
StringBuffer paramsStr = new StringBuffer();
try {
paramsStr.append(URLEncoder.encode(gson.toJson(object),
"utf-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
URLConnection uc;
switch (method) {
case POST:
uc = new URL(url).openConnection();
uc.setDoOutput(true);
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(uc.getOutputStream(),
Config.CHARSET));
bw.write(paramsStr.toString());
bw.flush();
break;
default:
uc = new URL(url + "?" + paramsStr.toString())
.openConnection();
break;
}
Log.d("net", "Resquest url:" + uc.getURL());
Log.d("net",
"post json:"
+ URLDecoder.decode(paramsStr.toString(),
"utf-8"));
BufferedReader brBufferedReader = new BufferedReader(
new InputStreamReader(uc.getInputStream(),
Config.CHARSET));
String line = null;
StringBuffer result = new StringBuffer();
while ((line = brBufferedReader.readLine()) != null) {
result.append(line);
}
Log.d("net", "result json:" + result);
return result.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
if (successCallback != null) {
successCallback.onSuccess(result);
}
} else {
if (failCallback != null) {
failCallback.onFail();
}
}
super.onPostExecute(result);
}
}.execute();
}
public static interface SuccessCallback {
void onSuccess(String result);
}
public static interface FailCallback {
void onFail();
}
}