weixin.popular.client.XmlResponseHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weixin-popular Show documentation
Show all versions of weixin-popular Show documentation
The weixin-popular is a JAVA SDK for weixin. Weixin web url is https://mp.weixin.qq.com.
package weixin.popular.client;
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weixin.popular.util.XMLConverUtil;
public class XmlResponseHandler{
private static Logger logger = LoggerFactory.getLogger(XmlResponseHandler.class);
public static ResponseHandler createResponseHandler(Class clazz){
return new XmlResponseHandlerImpl(null, clazz);
}
public static class XmlResponseHandlerImpl extends LocalResponseHandler implements ResponseHandler {
private Class clazz;
public XmlResponseHandlerImpl(String uriId, Class clazz) {
this.uriId = uriId;
this.clazz = clazz;
}
@Override
public T handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity);
Header contentType = response.getEntity().getContentType();
if(contentType!=null&&!contentType.toString().matches(".*[uU][tT][fF]-8$")){
str = new String(str.getBytes("iso-8859-1"),"utf-8");
}
logger.info("URI[{}] elapsed time:{} ms RESPONSE DATA:{}",super.uriId,System.currentTimeMillis()-super.startTime,str);
return XMLConverUtil.convertToObject(clazz,str);
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
}
}