1、Android获取SSH框架服务器JSP文件内容 可以通过HttpClient,然后用HtmlParser去解析想要的内容。其中getCommentData(String url),url为想要抓取的页面地址。 注意:往往获取网页内容时,格式为UTF-8,直接拿数据时,往往会是“ <”格式,这时候需要通过StringEscapeUtils.unescapeHtml()处理。- package support;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import logic.MainService;
-
- import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
- import org.apache.commons.httpclient.HttpException;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.params.HttpMethodParams;
- import org.apache.commons.lang.StringEscapeUtils;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.htmlparser.Node;
- import org.htmlparser.NodeFilter;
- import org.htmlparser.Parser;
- import org.htmlparser.filters.AndFilter;
- import org.htmlparser.filters.HasAttributeFilter;
- import org.htmlparser.filters.TagNameFilter;
- import org.htmlparser.util.NodeList;
- import org.htmlparser.util.ParserException;
- import org.htmlparser.util.SimpleNodeIterator;
-
- public class CommentData {
- private List<Map<String, String>> placedatalist ;
-
- public List<Map<String, String>> getPlacedatalist() {
- return placedatalist;
- }
- public void setPlacedatalist(List<Map<String, String>> placedatalist) {
- this.placedatalist = placedatalist;
- }
- private String getNoAHref(String source){
- return source.substring(source.indexOf("\"", source.indexOf("href"))+1, source.lastIndexOf("\""));
- }
-
- private String getAImageSrc(String source){
- return source.substring(source.indexOf("\"", source.indexOf("src"))+1, source.lastIndexOf("\""));
- }
- public List<Map<String, String>> getCommentData(String url) throws ParserException{
- placedatalist= new ArrayList<Map<String, String>>();
-
- HttpGet get=new HttpGet(url);
- try{
- HttpResponse httpReponse=MainService.guiji.getHttpClient().execute(get);
- HttpEntity entity=httpReponse.getEntity();
-
- BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(),"utf-8"));
- String line = null;
- String webcontent="";
- while ((line = br.readLine()) != null)
- {
- webcontent+=line;
- }
- br.close();
-
- Parser parser=new Parser(webcontent);
- NodeFilter filter1 = new TagNameFilter("table");
- NodeFilter filter2 = new HasAttributeFilter("id","idcomment");
- NodeFilter filter = new AndFilter(filter1,filter2);
- NodeList nodes = parser.extractAllNodesThatMatch(filter);
- SimpleNodeIterator iterator = nodes.elements();
- while (iterator.hasMoreNodes()) {
- Map<String, String> datamap = new HashMap<String, String>();
- datamap.put("userlogo", null);
- datamap.put("userlink", null);
- datamap.put("idmsgcontent", null);
- datamap.put("idmsgpic", null);
- datamap.put("idtime", null);
- datamap.put("idplace", null);
- datamap.put("iddeleteReply", null);
- datamap.put("idreply", null);
-
-
- Node node = iterator.nextNode();
- Parser parser01=new Parser(node.toHtml());
-
- NodeList chilenodes2 = parser01.extractAllNodesThatMatch(new TagNameFilter("a"));
- Node tempnode=null;
-
- for(int i=0;i<chilenodes2.size();i++){
- tempnode=chilenodes2.elementAt(i);
-
- if(tempnode.getText().contains("iduserlogo")){
- datamap.put("userlogo", getAImageSrc(tempnode.toHtml()));
- datamap.put("userlink", getNoAHref(tempnode.getText()));
- continue;
- }
- if(tempnode.getText().contains("idmsgcontent")){
- datamap.put("idmsgcontent", StringEscapeUtils.unescapeHtml(tempnode.toPlainTextString()));
- continue;
- }
- if(tempnode.getText().contains("idmsgpic")){
- datamap.put("idmsgpic", getAImageSrc(tempnode.toHtml()));
- continue;
- }
- if(tempnode.getText().contains("idtime")){
- datamap.put("idtime", StringEscapeUtils.unescapeHtml(tempnode.toPlainTextString()));
- continue;
- }
- if(tempnode.getText().contains("idplace")){
- datamap.put("idplace", StringEscapeUtils.unescapeHtml(tempnode.toPlainTextString()));
- continue;
- }
- if(tempnode.getText().contains("iddeleteReply")){
- datamap.put("iddeleteReply", getNoAHref(tempnode.getText()));
- continue;
- }
- if(tempnode.getText().contains("iddeleteReply")){
- datamap.put("iddeleteReply", getNoAHref(tempnode.getText()));
- continue;
- }
- if(tempnode.getText().contains("idreply")){
- datamap.put("idreply", getNoAHref(tempnode.getText()));
- continue;
- }
-
- }
-
- placedatalist.add(datamap);
- }
-
-
- }
- catch (HttpException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- } finally {
-
- }
- return placedatalist;
- }
- }
复制代码 2、Android客户端数据提交给SSH框架服务器
通过使用将数据存入MultipartEntity中,这种方式可以上传文件、字符串;在提交包含中文的字符串时,需要在addPart后面加入Charset.forName(HTTP.UTF_8),这样不会导致服务器接收乱码。 HttpPost post=new HttpPost(SystemConstant.baseURL+"actionname"); 其中SystemConstant.baseURL为网站的域名,类似http://172.22.192.37:8888/XXX/,actionname为提交数据到指定的Action名称。
当然JSESSIONID为SSH框架服务器cookie名称,根据不同框架修改这个名称。- public boolean publishMood(Map<String,String> data){
- HttpClient httpClient=new DefaultHttpClient();
- HttpPost post=new HttpPost(SystemConstant.baseURL+"publishMood");
- //如果有数据的话就直接发过去,没有的就不用发了,服务器那边自动会生成数据
- try{
- MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null,Charset.forName(HTTP.UTF_8));
- if(data.get("avator")!=null){
- compressImage((String) data.get("avator"));
- File file=new File((String) data.get("avator"));
- multipartEntity.addPart("moodPic",new FileBody(file));
- }
-
- multipartEntity.addPart("messageInfo.msg_content",new StringBody(data.get("msgcontent"),Charset.forName(HTTP.UTF_8)));
-
- if(data.get("moodvalue")!=null)
- multipartEntity.addPart("mood",new StringBody( data.get("moodvalue")));
-
- multipartEntity.addPart("messageInfo.coordinate",new StringBody(MainService.guiji.getCoordinate()));
-
- post.getParams().setParameter("http.protocol.cookie-policy",CookiePolicy.BROWSER_COMPATIBILITY);
- post.setEntity(multipartEntity);
- if(JSESSIONID!=null){
- post.setHeader("Cookie","JSESSIONID=" + JSESSIONID);
- }
- HttpResponse response=httpClient.execute(post);
- if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
- CookieStore mCookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
- List<Cookie> cookies = mCookieStore.getCookies();
- for (int i = 0; i < cookies.size();i++)
- {
- if ("JSESSIONID".equals(cookies.get(i).getName())) {
- JSESSIONID = cookies.get(i).getValue();
- break;
- }
-
- }
- return true;
- }
- }catch (Exception e) {
-
- }
- return false;
-
- }
复制代码
|