Http远程调取,返回值有string标签包裹解决

package io.jboot.ehospital.service.provider;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

/**
 * Created by jj on 2017/6/20.
 * 因为调用接口返回的xmlstring 包裹,例子如下:
 * <string xmlns="http://tempuri.org/">
 *     <Response>
 *         <ResultCode>0</ResultCode>
 *         <ErrorMsg>成功</ErrorMsg>
 *     </Response>
 * </string>
 */
@XmlRootElement( name = "string", namespace = "http://tempuri.org/" )
@XmlAccessorType( XmlAccessType.FIELD )
public class XmlString {

    @XmlValue
    private String string;

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }
}





@Override
public XtResult XtAddPrescription (String url,String parameter){
JAXBContext context = null ;
Unmarshaller unmarshaller = null ;
XmlString xmlString = null ;
XtResult xtResult= null ;
try {
parameter = URLEncoder. encode (parameter);
String regular = HttpClientUtil.getRegular(url+"?strJson="+parameter);
context = JAXBContext. newInstance (XmlString. class , XtResult. class );
unmarshaller = context.createUnmarshaller();
xmlString = (XmlString) unmarshaller.unmarshal( new StringReader(regular));
String string = xmlString.getString();
xtResult = JSONObject. parseObject (string, XtResult. class );
} catch (Exception e) {
e.printStackTrace();
}
return xtResult;
}


package io.jboot.web.common.util;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by KC.ZQ on 2017/10/25.
 */
public class HttpClientUtil {

    public static String getRegular(String url){
        // 需要访问的接口路径
        // 配置请求信息(请求时间)
        RequestConfig rc = RequestConfig.custom().setSocketTimeout(5000)
                .setConnectTimeout(5000).build();
        // 获取使用DefaultHttpClient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 返回结果
        String result = null;
        try {
            if (url != null) {
                // 创建HttpGet对象,将URL通过构造方法传入HttpGet对象
                HttpGet httpget = new HttpGet(url);
                // 将配置好请求信息附加到http请求中
                httpget.setConfig(rc);
                // 执行DefaultHttpClient对象的execute方法发送GET请求,通过CloseableHttpResponse接口的实例,可以获取服务器返回的信息
                CloseableHttpResponse response = httpclient.execute(httpget);
                try {
                    // 得到返回对象
                    org.apache.http.HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        // 获取返回结果
                        result = EntityUtils.toString(entity);
                        return result;

                    }
                } finally {
                    // 关闭到客户端的连接
                    response.close();
                }
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭http请求
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值