TestGateWay.java 1.99 KB
package com.diligrp.getway.test;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.diligrp.mobsite.getway.domain.protocol.saler.order.GetOutOrderListReq;
import com.diligrp.mobsite.getway.domain.protocol.saler.order.GetOutOrderListResp;
import com.diligrp.titan.sdk.http.BasicHttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

/**
 * Created by david on 2015/10/30.
 */
public class TestGateWay {

	private final static String ENCODE = "UTF8";


	public static void main(String [] args) {

		for (int i = 0 ; i< Long.parseLong(args[0]);i++) {

			GetOutOrderListReq req = new GetOutOrderListReq();
//			req.setGuardId(248l);
//			req.setBuyerId(4154l);
			req.setGuardId(Long.parseLong(args[1]));
			req.setBuyerId(Long.parseLong(args[2]));
			req.setTimeEnd(1446134400000l);

			GetOutOrderListResp output = new GetOutOrderListResp();
			output = execute("http://mobapi.nong12.com/mobsiteApp/outorder/order/getOrders.do", req, output, new TypeReference<GetOutOrderListResp>() {
			});
			System.out.println(output);

		}

	}



	private static <T extends GetOutOrderListResp> T execute(String path, Object paramObj, T output, TypeReference type) {
		try {
			HttpPost post = new HttpPost(path);
			post.addHeader("Content-type", "application/json");
			String body = JSON.toJSONString(paramObj);
			StringEntity entity = new StringEntity(body, ENCODE);
			post.setEntity(entity);

			HttpResponse response = BasicHttpClient.getHttpClient(0).execute(post);
			int status = response.getStatusLine().getStatusCode();
			if (status == HttpStatus.SC_OK) {
				String message = EntityUtils.toString(response.getEntity(), ENCODE);
				output = (T) JSON.parseObject(message, type);
			} else {
				output.setCode(status);
			}
		} catch (Exception e) {
			System.err.println(e);
			output.setMsg(e.getMessage());
		}
		return output;
	}
}