Sax解析......输出没有完成

时间:2013-02-04 10:14:52

标签: android parsing sax

在解析这个时我无法获得所有属性。我只是得到这个名字,任何人都可以帮助我。没有gt我错了.........

 public class Xmlfile extends Activity {
// For Single Data
private List<String> item = new ArrayList<String>();
public ArrayList<Gettersandsetters> data = new ArrayList<Gettersandsetters>();

Xmlfile x;

private boolean in_name;
private boolean in_formatted_address;
private boolean in_lat;
private boolean in_lng;
Gettersandsetters d;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    x = this;
    new StudentTask().execute();
}

public class StudentTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = ProgressDialog.show(x, "Please Wait", "Loading Data");

        pd.setCancelable(true);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {

            URL u = new URL(
                    "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+eF5s");
            InputSource i = new InputSource(u.openStream());

            SAXParserFactory sx = SAXParserFactory.newInstance();
            SAXParser msx = sx.newSAXParser();

            XMLReader reader = msx.getXMLReader();

            MyXmlHandler myRSSHandler = new MyXmlHandler();

            reader.setContentHandler(myRSSHandler);
            // Reading xml file from raw folder.
            // InputStream istream = null;
            // istream =
            // getApplicationContext().getResources().openRawResource(R.raw.studentdetails);

            // InputSource myInputSource = new
            // InputSource(rssUrl.openStream());

            // myXMLReader.parse(myInputSource);
            reader.parse(new InputSource(u.openStream()));

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        // setListAdapter(new
        // ArrayAdapter<String>(x,android.R.layout.simple_list_item_1,
        // item));
        // StudentDetails d = new StudentDetails();
        // data.add(d);
        ListView l = (ListView) findViewById(R.id.list);
        Demo demo = new Demo();
        l.setAdapter(demo);
        l.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                if (data.get(position) != null) {
                    Intent in = new Intent(Xmlfile.this, Mapview.class);
                    startActivity(in);

                }
            }
        });

        l.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View v,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                if (data.get(position) != null) {
                    AlertDialog ab = new AlertDialog.Builder(
                            Xmlfile.this).create();


                    ab.setTitle("Restaurent Dtails");


                    //TextView name1 = (TextView) v.findViewById(R.id.name);
                    //name1.setText(data.get(position).getName());
                    ab.setMessage(data.get(position).getName()+""+data.get(position).getFormatted_address());

                    ab.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        // Write your code here to execute after dialog closed
                        Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
                        }
                });


                    ab.show();
                }

                return true;
            }
        });
        pd.dismiss();
    }

}

// 1)Implementing Handler to get data from xml file
public class MyXmlHandler extends DefaultHandler {
    private boolean in_name = false;
    private boolean in_formatted_address = false;
    private boolean in_lat = false;
    private boolean in_lng = false;
    private boolean in_result = false;

    // private boolean in_clas = false;
    // private boolean in_phno = false;
    // private boolean in_email = false;

    String name;
    String formatted_address;
    String lat, lng;

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        if (localName.equalsIgnoreCase("result")) {
            d = new Gettersandsetters();

            this.in_result = true;
        } else if (localName.equalsIgnoreCase("name")) {

            this.in_name = true;
        } else if (localName.equalsIgnoreCase("formatted_address")) {

            this.in_formatted_address = true;

        } else if (localName.equalsIgnoreCase("lat")) {

            this.in_lat = true;
        }

        else if (localName.equalsIgnoreCase("lng")) {

            this.in_lng = true;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (this.in_name) {

            name = new String(ch, start, length);
            d.setName(name);
             Log.v("name========", name + data.size());

        }

        else if (this.in_formatted_address) {

            formatted_address = new String(ch, start, length);
            d.setFormatted_address(formatted_address);
            Log.v("name========", formatted_address + data.size());
        } else if (this.in_lat) {

            lat = new String(ch, start, length);

            d.setLat(lat);

        }

        else if (this.in_lng) {

            lng = new String(ch, start, length);
            d.setLng(lng);

        }

    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        if (localName.equals("name")) {

            this.in_name = false;

        } else if (localName.equals("formatted_address")) {
            this.in_formatted_address = false;

        }

        else if (localName.equals("lat")) {
            this.in_lat = false;

        } else if (localName.equals("lng")) {
            this.in_lng = false;

        } else if (localName.equals("result")) {
            this.in_result = false;
            data.add(d);
        }

    }
}

public class Demo extends BaseAdapter

{
    Activity context;
    private List<String> s;

    public Demo() {
        super();
        this.context = context;
        this.s = s;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        // return LISTITEMS.size();
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {
            LayoutInflater vi = LayoutInflater.from(parent.getContext());
            v = vi.inflate(R.layout.activitymain, null);
        }

        final Gettersandsetters studentdetails = data.get(position);

        TextView name = (TextView) v.findViewById(R.id.name);
        TextView formatted_address = (TextView) v
                .findViewById(R.id.formatted_address);
        TextView lat = (TextView) v.findViewById(R.id.lat);
        TextView lng = (TextView) v.findViewById(R.id.lng);

        // c.moveToPosition(position);

        // img.setImageResource(Integer.parseInt(c.getString(0)));

        name.setText(data.get(position).getName());
        formatted_address
                .setText(data.get(position).getFormatted_address());
        Log.v("rformated address", ""+formatted_address);
        lat.setText(data.get(position).getLat());
        lng.setText(data.get(position).getLng());

        return v;
    }

}

Gettera和setter

package com.sudheer.webservices;

公共类Gettersandsetters {

public String name,formatted_address,lat,lng;


public Gettersandsetters(String n,String f,String la,String ln)
{
    this.name=n;
    this.formatted_address=f;
    this.lat=la;
    this.lng=ln;

}

public Gettersandsetters(){

}

public String getName() {
    return name;
}
public void setName(String name) {
       this.name = name;
   }

public String getFormatted_address() {
    return formatted_address;
}

public void setFormatted_address(String Formatted_address) {
       this.formatted_address = formatted_address;
   }
public String getLat() {
    return lat;
}
public void setLat(String Lat) {
       this.lat = lat;
   }

public String getLng() {
    return lng;
}


public void setLng(String Lng) {
       this.lng = lng;
   }

}

1 个答案:

答案 0 :(得分:0)

你有

是否正常
  

REQUEST_DENIED

当您到达您提供的网址时(https://maps.googleapis.com/maps/api/place/textsearch/xml?query=restaurants+eF5s)?

这可能是一个好的开始