0

Iam getting the data but i want to group some particular data in the form of a group such as in airport list namely

JFK in that all lat,long,name,code of the airport has to make one array similarly the other airports.Please help me the issue.

main java class:

 public String getAirportListHTTPURL(String mAirportLocation,Double airportLatitude, Double airportLongitude, String mAirlineCode, boolean mAirport2, String mAddress2, String mCity2, String mState2, String mCountry2) {
        String url = CarmelURLConstants.LIVE_URL.toString();
        URL u = new URL(url);
        URLConnection uc = u.openConnection();
        HttpURLConnection http = (HttpURLConnection) uc;

        http.setDoOutput(true);
        http.setDoInput(true);
        http.setRequestMethod("POST");
        http.setRequestProperty("Content-type", "text/xml; charset=utf-8");     

        String xmldata = Here i used the xml format Syntax data
        System.out.println(xmldata);
        OutputStream out = http.getOutputStream();
        Writer wout = new OutputStreamWriter(out);
        wout.write(xmldata);
        wout.flush();
        wout.close();
        // Reading Data from the server using getInputStream 
        BufferedReader rd = new BufferedReader(new InputStreamReader(http.getInputStream()));
        System.out.println("code..."+http.getResponseCode());

        String result, responsedata = " ";
        RequestName = "AirportlistByPickUpAddress Request:-";
        ResponseName = "AirportlistByPickupAddress Response:-";
        while ((result=rd.readLine()) != null) {
           System.out.println(result");  
           responsedata = result;

           // Method to parse in SAX Parser
         mParsedValue = ParseAirportLocationResponse(result);
        }
       // updateTraceFile(xmldata, responsedata,RequestName,ResponseName);
        }catch(Exception e){
            mParsedValue = e.getMessage();
            e.printStackTrace();
        }
        return mParsedValue;
    }

    private String ParseAirportLocationResponse(String result) {
        try {

             // Create a XMLReader from SAXParser
            XMLReader mXmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
            AirportHandler Airportlist = new AirportHandler();

            // Apply Handler to XML Reader
            mXmlReader.setContentHandler(Airportlist);
            // Start the Process to Parse
            InputSource is = new InputSource(new StringReader(result));
            mXmlReader.parse(is);
        } catch (Exception e) {
            System.out.println(e);

        }
            return result;// Get the Parsed Data

    }

In the XMLSETTERS CLASS:

 public class XMLGettersSetters {
 public static ArrayList<String> airportCode = new ArrayList<String>();
 public static  ArrayList<String> airportName = new ArrayList<String>();
 public static ArrayList<String> latitude = new ArrayList<String>();
 public static ArrayList<String> longitude = new ArrayList<String>();
public ArrayList<String> getlongitude() {
    return longitude;
}
public void setlongitude(String longitude) {
    this.longitude.add(longitude);
    Log.i("This is the longitude:", longitude);
}
public ArrayList<String> getairportCode() {
    return airportCode;
}

public void setairportCode(String airportCode) {
    this.airportCode.add(airportCode);
    Log.i("This is the airportCode:", airportCode);
}
public ArrayList<String> getairportName() {
    return airportName;
}
public void setairportName(String airportName) {
    this.airportName.add(airportName);
    Log.i("This is the airportName:", airportName);
}
public ArrayList<String> getlatitude() {
    return latitude;
}
public void setlatitude(String latitude) {
    this.latitude.add(latitude);
    Log.i("This is the latitude:", latitude);
}

}

In the Airport handler class:

public class AirportHandler extends DefaultHandler {
String elementValue = null;
Boolean elementOn = false;
public static XMLGettersSetters data = null;

public static XMLGettersSetters getXMLData() {
    return data;
}

public static void setXMLData(XMLGettersSetters data) {
    AirportHandler.data = data;
}
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    elementOn = true;

    if (localName.equals("getAirportListByPickUpAddressResponse"))
    {
        data = new XMLGettersSetters();
    } 
    else if (localName.equals("airportList")) {

    }
}

/** 
 * This will be called when the tags of the XML end.
 **/
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    elementOn = false;

    /** 
     * Sets the values after retrieving the values from the XML tags
     * */ 
    if (localName.equalsIgnoreCase("airportCode"))
        data.setairportCode(elementValue);
    else if (localName.equalsIgnoreCase("airportName"))
        data.setairportName(elementValue);
    else if (localName.equalsIgnoreCase("latitude"))
        data.setlatitude(elementValue);
    else if (localName.equalsIgnoreCase("longitude"))
        data.setlongitude(elementValue);

}

/** 
 * This is called to get the tags value
 **/
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (elementOn) {
        elementValue = new String(ch, start, length);
        elementOn = false;
    }

}
}
4

1 に答える 1

1

XML解析を実行した後、次のようにしてください

ArrayList<String> aList = new ArrayList<String>();

aList.add("your string");
于 2013-03-26T08:46:29.573 に答える