快递100 请求以及回调

请求

package test2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.junit.Test;

import net.sf.json.JSONArray;

public class doTest {

    @Test
    public void test1(){
        HttpRequest req = new HttpRequest();
        req.setCompany("yunda");
        req.setFrom("上海市浦东新区");
        req.setTo("广东市深圳南山区");
        req.setNumber("12345678");
        Parameters pt = new Parameters();
        pt.setCallbackurl("http://xxxx/manage/order/sys/rollbackKuaiDi100");
        pt.setDepartureCom("yunda");
        req.setParameters(pt);
        req.setKey("xxxx");

        HashMap<String, String> p = new HashMap<String, String>();
        p.put("schema", "json");
        p.put("param", JsonUtils.objectToJson(req));
        try {
            String resp = postData("http://poll.kuaidi100.com/poll", p, "utf-8").toString();
            System.out.println(resp);
            //格式化数据
            JSONArray json = JSONArray.fromObject(resp);//userStr是json字符串
            System.out.println(json);
            List<HttpResponse> httpResponse= (List<HttpResponse>)JSONArray.toCollection(json, HttpResponse.class);
            if(httpResponse.get(0).isResult()==true){
                System.out.println("订阅成功");
            }else{
                System.out.println("订阅失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 发送请求
     * @param url
     * @param params
     * @param codePage
     * @return
     * @throws Exception
     */
    public String postData(String url, Map<String, String> params, String codePage) throws Exception {

        final HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(25 * 1000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30 * 1000);

        final PostMethod method = new PostMethod(url);
        if (params != null) {
            method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, codePage);
            method.setRequestBody(assembleRequestParams(params));
        }
        String result = "";
        try {
            httpClient.executeMethod(method);
            result = new String(method.getResponseBody(), codePage);
        } catch (final Exception e) {
            throw e;
        } finally {
            method.releaseConnection();
        }
        return result;
    }
    /**
     * 组装http请求参数
     * 
     * @param params
     * @param menthod
     * @return
     */
    private synchronized static NameValuePair[] assembleRequestParams(Map<String, String> data) {
        final List<NameValuePair> nameValueList = new ArrayList<NameValuePair>();

        Iterator<Map.Entry<String, String>> it = data.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
            nameValueList.add(new NameValuePair((String) entry.getKey(), (String) entry.getValue()));
        }

        return nameValueList.toArray(new NameValuePair[nameValueList.size()]);
    }



}

回调

package test3;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

import net.sf.json.JSONObject;

public class test{
    @RequestMapping("/rollbackKuaiDi100")
    @ResponseBody
    public Object  rollbackKuaiDi100(HttpServletRequest request,HttpServletResponse response){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("result",false);
            map.put("returnCode","500");
            map.put("message","保存失败");
            try {
                String param = request.getParameter("param");
                JSONObject  jsonObject = JSONObject.fromObject(param);
                System.out.println(param);
                /**
                 * 取出需要的返回值,然后进行业务操作
                 */
//              OrderExpress orderExpress = orderExpressService.selectOrderExpressByOrderCode(jsonObject.getString("nu"));
//              orderExpress.setStatus(jsonObject.getString("status"));
//              orderExpress.setMessage(jsonObject.getString("message"));
//              orderExpress.setData(jsonObject.getJSONObject("data"));
//              JSONObject jsonResult =(JSONObject) jsonObject.get("lastResult");
//              orderExpress.setState(Integer.parseInt(jsonResult.getString("state"))); 
//              orderExpress.setExpressCode(jsonObject.getString("nu"));//快递单号
//              orderExpressService.saveOrUpdateOrderExpress(orderExpress);

                // 处理快递结果

                map.put("result",true);
                map.put("returnCode","200");
                map.put("message","保存成功");
                //这里必须返回,否则认为失败,过30分钟又会重复推送。
                response.getWriter().print(JSONObject.fromObject(map));
            } catch (Exception e) {
                map.put("message","保存失败" + e.getMessage());
                //保存失败,服务端等30分钟会重复推送。
                try {
                    response.getWriter().print(JSONObject.fromObject(map));
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            return "success";
        }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值