Util.java :  » UnTagged » smartresearch » com » andromeda » smartresearch » util » Android Open Source

Android Open Source » UnTagged » smartresearch 
smartresearch » com » andromeda » smartresearch » util » Util.java
package com.andromeda.smartresearch.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.andromeda.smartresearch.vo.QuestionVO;
import com.andromeda.smartresearch.vo.ResearchVO;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Util {
  static public final int RETURN_TYPE_STRING = 1;
  static public final int RETURN_TYPE_ARRAYLIST_RESEARCHVO = 2;
  static public final int RETURN_TYPE_ARRAYLIST_QUESTIONVO = 3;
  static public void ConnectServer(String url, ArrayList<NameValuePair> parameters, Handler handler, int parsingResultType) {
    final Handler handle = handler;
    final int resultType= parsingResultType;
    final ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

      @Override
      public String handleResponse(HttpResponse response)
      throws ClientProtocolException, IOException {

        Log.i("ResponseHandler" , "handleResponse()");
        String result = null;
        HttpEntity entity = response.getEntity();         
        Message message =handle.obtainMessage();
        Bundle bundle = new Bundle();

        bundle.putSerializable("RESULT", parsingData(entity.getContent(), resultType));
        message.setData(bundle);
        handle.sendMessage(message);
        return result;
      }
    };

    final String address = url;
    final ArrayList<NameValuePair> parameterList = parameters;
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          HttpClient http = new DefaultHttpClient();
          HttpParams params = http.getParams();
          params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
          HttpConnectionParams.setConnectionTimeout(params, 5000);
          HttpConnectionParams.setSoTimeout(params, 5000);


          HttpPost httpPost = new HttpPost(address);
          UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(parameterList,"UTF-8");
          httpPost.setEntity(encodedFormEntity);
          http.execute(httpPost, responseHandler);

        } catch (Exception e) {
          e.printStackTrace();
        }
      }    
    }).start();    
  }

  static Serializable parsingData(InputStream inputStream, int parsingType){
    Serializable parsingResult=null;
    switch(parsingType){
    case 1: // returnType : String
      try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(new InputStreamReader(inputStream));
        while(parser.next()!=XmlPullParser.END_DOCUMENT){
          String name = parser.getName();
          if(name != null && name.equals("result")){
            parsingResult=parser.nextText();
            break;
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return parsingResult;
    case 2: // returnType : ArrayList<ResearchVO>
      ArrayList<ResearchVO> parsedResearchList = new ArrayList<ResearchVO>();
      try {
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=factory.newDocumentBuilder();
        Document document=builder.parse(inputStream);

        Element root= document.getDocumentElement(); 

        NodeList researches=root.getElementsByTagName("research"); 

        int item_count=researches.getLength(); 

        for(int i=0; i<item_count; i++){
          ResearchVO vo = new ResearchVO();

          Node research=researches.item(i); 

          Element element = (Element) research;
          String no = element.getAttribute("no");
          vo.setNo(Integer.parseInt(no));

          NodeList childList = element.getChildNodes();
          final int childCount = childList.getLength();

          Node child = null;
          String value = null;
          String nodeName = null;

          for(int j=0; j<childCount; j++){
            child = childList.item(j);
            if(child.getNodeType()==Node.ELEMENT_NODE){
              nodeName = child.getNodeName();
              value = child.getFirstChild().getNodeValue();
            }else{
              continue;
            }

            if(nodeName.equals("category")){
              vo.setCategory(value);
            }else if(nodeName.equals("subject")){
              vo.setSubject(value);
            }else if(nodeName.equals("startday")){
              vo.setStartDay(value);
            }else if(nodeName.equals("authority")){
              vo.setAuthority(value);
            }else if(nodeName.equals("endday")){
              vo.setEndDay(value);
            }

          }
          parsedResearchList.add(vo);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return parsedResearchList;
    case 3: // returnType : ArrayList<QuestionVO>
      ArrayList<QuestionVO> parsedQuestionList = new ArrayList<QuestionVO>();
      try {
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=factory.newDocumentBuilder();
        Document document=builder.parse(inputStream);

        Element root= document.getDocumentElement();

        NodeList qustiones=root.getElementsByTagName("qustion"); 

        int item_count=qustiones.getLength(); 

        for(int i=0; i<item_count; i++){
          QuestionVO questionVO = new QuestionVO();

          Node qustion=qustiones.item(i); 

          Element element = (Element) qustion;
          String no = element.getAttribute("no");
          questionVO.setQuestionNO(no);

          NodeList childList = element.getChildNodes();
          final int childCount = childList.getLength();

          Node child = null;
          String value = null;
          String nodeName = null;

          for(int j=0; j<childCount; j++){
            child = childList.item(j);
            if(child.getNodeType()==Node.ELEMENT_NODE){
              nodeName = child.getNodeName();
              value = child.getFirstChild().getNodeValue();
            }else{
              continue;
            }

            if(nodeName.equals("subject")){
              questionVO.setQuestionSubject(value);
            }else if(nodeName.equals("type")){
              questionVO.setQuestionType(value);
            }else if(nodeName.equals("value")){
              questionVO.setQuestionValue(value);
            }
          }
          parsedQuestionList.add(questionVO);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return parsedQuestionList;
    case 4:
      break;
    case 5:
      break;
    }
    return null;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.