将webserver接口转JSON 方便测试

 


package test;



import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;




/**

 * 模拟手机客户端请求服务

 * 需要的包有
 *  commons-beanutils-1.8.3.jar
 * commons-collections-3.2.1.jar
 * commons-httpclient.jar
 * commons-lang-2.5.jar
 * ezmorph-1.0.6.jar
 * json-lib-2.3.jar
 * 加密 和压缩 另外还要其他的包
 * @author andy.wang
 * 
 */
public class Test {


private static String uuid = "F3A24625-9F56-49D1-BC0A-8C5DCA99B92B";
private static String partner = "amwayhub";
private static Long ada = new Long(16808);
private static String keySuffix = "an.D9_";
private static String test_url="http://10.140.164.86:80/crm/test";


private static void testFuntion() throws Exception {
String url = test_url + "?p=" + partner + "&u="
+ URLEncoder.encode(uuid) + "&s=mpayment.syncustomers"+"&a="+ada;
net.sf.json.JSONObject json = new net.sf.json.JSONObject();
String jsonStr =  "{\n" + 
"\"lastSyncTime\" : 1394436088000,\n" + 
"\"MD5Key\" : \"\",\n" + 
"\"customerArray\" : [\n" + 
"  {\n" + 
" \"email\" : \"\",\n" + 
" \"customerStatus\" : \"\",\n" + 
" \"officeAddress\" : \"\",\n" + 
" \"spouseName\" : \"\",\n" + 
" \"groupServerId\" : \"\",\n" + 
" \"hobbies\" : \"\",\n" + 
" \"expirationDate\" : 0,\n" + 
" \"terminalGroupId\" : \"3C0F6483-5FED-4044-97CF-385153B8BE24\",\n" + 
" \"weibo\" : \"\",\n" + 
" \"tel\" : \"\",\n" + 
" \"serverId\" : \"\",\n" + 
" \"weChat\" : \"\",\n" + 
" \"customerType\" : \"\",\n" + 
" \"birthday\" : 0,\n" + 
" \"idCard\" : \"\",\n" + 
" \"county\" : \"\",\n" + 
" \"sex\" : \"\",\n" + 
" \"prefecture\" : \"\",\n" + 
" \"mobile\" : \"14253636254\",\n" + 
" \"headPortrait\" : \"\",\n" + 
" \"name\" : \"mobilecend\",\n" + 
" \"ada\" : \"0\",\n" + 
" \"product\" : \"\",\n" + 
" \"field5\" : \"\",\n" + 
" \"jobType\" : \"\",\n" + 
" \"status\" : \"1\",\n" + 
" \"updateTime\" : 0,\n" + 
" \"field4\" : \"\",\n" + 
" \"groupTerminalId\" : \"3C0F6483-5FED-4044-97CF-385153B8BE24\",\n" + 
" \"province\" : \"\",\n" + 
" \"homeAddress\" : \"\",\n" + 
" \"fax\" : \"\",\n" + 
" \"jobTitle\" : \"\",\n" + 
" \"field3\" : \"2137ca89-3251-4149-a9da-42f705c02408\",\n" + 
" \"customerIdentity\" : \"\",\n" + 
" \"qq\" : \"\",\n" + 
" \"maritalStatus\" : \"\",\n" + 
" \"weddingDay\" : 0,\n" + 
" \"field2\" : \"0\",\n" + 
" \"childStatus\" : \"\",\n" + 
" \"note\" : \"\",\n" + 
" \"terminalId\" : \"8BB6471F-364E-43EA-B014-F00C8F2239AA\"\n" + 
"  }\n" + 
"],\n" + 
"\"appCode\" : \"1001\",\n" + 
"\"customerGroupArray\" : [\n" + 
"   ],\n" + 
"\"currentTime\" : 1394438237000,\n" + 
"\"scheduleArray\" : [\n" + 
"  {\n" + 
" \"note\" : \"\",\n" + 
" \"updateTime\" : 0,\n" + 
" \"remind\" : \"\",\n" + 
" \"customerMobile\" : \"14253636254\",\n" + 
" \"type\" : \"拜访\",\n" + 
" \"title\" : \"baifang ing\",\n" + 
" \"isComplete\" : 0,\n" + 
" \"customerServerId\" : \"\",\n" + 
" \"terminalId\" : \"CE6EC535-09EB-414C-BF4E-B122AB3CBF63\",\n" + 
" \"serverId\" : \"\",\n" + 
" \"startTime\" : 1394438149000,\n" + 
" \"customerTerminalId\" : \"8BB6471F-364E-43EA-B014-F00C8F2239AA\",\n" + 
" \"repeat\" : \"\",\n" + 
" \"endTime\" : 1394441749000,\n" + 
" \"ada\" : \"0\",\n" + 
" \"status\" : \"1\"\n" + 
"  }\n" + 
"],\n" + 
"\"ada\" : \"16808\"\n" + 
"}";
json = net.sf.json.JSONObject.fromObject(jsonStr);


// SyncCustomerRequestDTO dto = new SyncCustomerRequestDTO();

// dto.setAppCode("1001");
// dto.setCurrentTime(new Long(System.currentTimeMillis()));
// dto.setCustomerArray(new CustomerDTO[0]);
// CustomerGroupDTO group = new CustomerGroupDTO();
// group.setTerminalId("shadow2");
// group.setGroupName("2");
// group.setSortCode("1");
// group.setStatus("1");
// group.setUpdateTime(new Long(System.currentTimeMillis()));
// dto.setCustomerGroupArray(new CustomerGroupDTO[] { group });
// dto.setScheduleArray(new CustomerScheduleDTO[0]);
// json = net.sf.json.JSONObject.fromObject(dto);

//
// JSONObject json = new JSONObject();
// json.put("dst_typ_cde", "SA");
// json.put("month", "201310");
// json.put("shop_code","SC11");
// json.put("shop_name","韶关店铺");
// json.put("province","广东省");
// json.put("pageno", 1);
// json.put("pagerownum", 10);
// json.put("month", "201311");
// json.put("queryada", 6);
//json.put("shop_code", "SC21");
//json.put("action", "2");
// json.put("pcApplyID",new Long(9555));
// json.put("payMethod","H");
// json.put("applyBirDte","19700101");
//    json.put("shopName","淮南店铺");
//    json.put("email","");
//    json.put("knownAmAppr","2");
//    json.put("applyCerFile1Name","678909.jpg");
//    json.put("spouseSex","");
//    json.put("spouseBirDte","");
//    json.put("pcApplyID",9593);
//   // json.put("spouseCerTyp",null);
//    json.put("spouseName","");
//    json.put("applyCerNo","678909");
//    json.put("shopPrv","安徽省");
//    json.put("tel","13661021461");
//    json.put("applyCerFile2","");
//    json.put("addrPrv","安徽省");
//    json.put("addr","欲uiii");
//    json.put("telPre","");
//    json.put("shopCde","EC4F");
//    json.put("rsv1","傅南生");
//    json.put("mobile","13661021461");
//    json.put("spouseCerFile1Name","");
//    json.put("applyChannel","9");
//    json.put("applyCerFile2Name","");
//   // json.put("ada",null);
//    json.put("spouseCerFile2","");
//    json.put("applyName","草稿測試");
//    json.put("spouseCerNo","");
//    json.put("matRecvMeth","1");
//    json.put("applyCerTyp","99");
//    json.put("postalCde","100083");
//    json.put("status",0);
//    json.put("spouseCerFile2Name","");
//    json.put("rsv2","吴彩云");
//    json.put("efileInd","N");
//   // json.put("preAda",null,);
//    json.put("introAda","211021");
//    json.put("spouseCerFile1","");
//    json.put("addrCity","合肥");
//    json.put("suppCnt",0);
//    json.put("applyCerFile1","20131108/678909_0.jpg");
//    json.put("applySex","M");
//    json.put("printPackMeth","1");


System.out.println(json.toString());
// 压缩
// byte[] bytes = MobileServiceHelper.zip(json.toString().getBytes(
// "utf-8"));
//
// // 加密
// String key = token.substring(0, 8) + keySuffix;
// bytes = MobileServiceHelper.encrypt(bytes, key);


// 发送请求
HttpURLConnection conn = (HttpURLConnection) new URL(url)
.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("content-type", "text/html");
conn.getOutputStream().write(json.toString().getBytes(
"utf-8"));
conn.getOutputStream().flush();
conn.getOutputStream().close();
// 收到请求
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024]; // buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = conn.getInputStream().read(buff, 0, 1024)) > 0) {
swapStream.write(buff, 0, rc);
}
// bytes = swapStream.toByteArray();
//
// // 解密
// bytes = MobileServiceHelper.decrypt(bytes, key);
//
// // 解压
// bytes = MobileServiceHelper.unzip(bytes);


String s = new String(swapStream.toByteArray(), "UTF-8");
System.out.println(s);
}

public static void main(String[] args) throws Exception {
System.out.print(new Date().getTime());
System.out.println("=============================");
testFuntion();
}


}



package test;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;


import com.amway.crm.webservice.customer.Customer;
import com.amway.crm.webservice.customer.CustomerGroup;
import com.amway.crm.webservice.customer.Schedule;
import com.amway.crm.webservice.customer.SyncCustomerRequest;
import com.amway.crm.webservice.customer.SyncCustomerResponse;
import com.amway.crm.webservice.customer.SyncCustomerSchedule;
import com.amway.crm.webservice.customer.SyncCustomerScheduleBindingImpl;


public class TestServlet extends HttpServlet {

/**

*/
private static final long serialVersionUID = 8466102047062771724L;

public SyncCustomerSchedule customer =new SyncCustomerScheduleBindingImpl();;


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

String ada = req.getParameter("a");
String request = new String(getBytesFromStream(req.getInputStream()),"utf-8");
net.sf.json.JSONObject  json = null;
try {
json = service(request,ada);
} catch (Exception e) {
System.out.println("testServlet error!"+e.getMessage());
}
resp.getWriter().write(json.toString());
resp.flushBuffer();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}

/**
* 从输入流中获取字节内容

* @param stream
* @return
* @throws IOException
*/
public static byte[] getBytesFromStream(InputStream stream)
throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024]; // buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = stream.read(buff, 0, 1024)) > 0) {
swapStream.write(buff, 0, rc);
}
return swapStream.toByteArray();
}

public  JSONObject service(String request,String ada) throws RemoteException {
net.sf.json.JSONObject netRequest = net.sf.json.JSONObject
.fromObject(request);
SyncCustomerRequest requestDTO = new SyncCustomerRequest();
requestDTO.setAda(ada);
if (null != netRequest.getString("appCode"))
requestDTO.setAppCode(netRequest.getString("appCode"));


if (netRequest.containsKey("currentTime"))
requestDTO.setCurrentTime(netRequest.getLong("currentTime"));


if (netRequest.containsKey("lastSyncTime"))
requestDTO.setLastSyncTime(netRequest.getLong("lastSyncTime"));
if (null != netRequest.getString("MD5Key"))
requestDTO.setMD5Key(netRequest.getString("MD5Key"));


if (null != netRequest.getJSONArray("customerGroupArray")) {
net.sf.json.JSONArray customerGroupJsonArray = netRequest
.getJSONArray("customerGroupArray");
CustomerGroup[] customerGroupDTOs = new CustomerGroup[customerGroupJsonArray
.size()];
for (int i = 0; i < customerGroupJsonArray.size(); i++) {
customerGroupDTOs[i] = (CustomerGroup) net.sf.json.JSONObject
.toBean(customerGroupJsonArray.getJSONObject(i),
CustomerGroup.class);
}
requestDTO.setCustomerGroupArray(customerGroupDTOs);
}
//---
if (null != netRequest.getJSONArray("customerArray")) {
net.sf.json.JSONArray customerJsonArray = netRequest
.getJSONArray("customerArray");
Customer[] customerDtos = new Customer[customerJsonArray
.size()];
for (int i = 0; i < customerJsonArray.size(); i++) {
customerDtos[i] = (Customer) net.sf.json.JSONObject.toBean(
customerJsonArray.getJSONObject(i), Customer.class);
}
requestDTO.setCustomerArray(customerDtos);
}
// --
if (null != netRequest.getJSONArray("scheduleArray")) {
net.sf.json.JSONArray customerScheduleJsonArray = netRequest
.getJSONArray("scheduleArray");
Schedule[] customerScheduleDTOs = new Schedule[customerScheduleJsonArray
.size()];
for (int i = 0; i < customerScheduleJsonArray.size(); i++) {
customerScheduleDTOs[i] = (Schedule) net.sf.json.JSONObject
.toBean(customerScheduleJsonArray.getJSONObject(i),
Schedule.class);
}
requestDTO.setScheduleArray(customerScheduleDTOs);
}
System.out.print(net.sf.json.JSONObject.fromObject(requestDTO).toString());
SyncCustomerResponse dto = customer.syncCustomer(requestDTO);
net.sf.json.JSONObject r =  net.sf.json.JSONObject.fromObject(dto);
return r;
}




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值