package cn.llbb.testpullxml; import android.support.v7.app.ActionBarActivity; import android.util.Log; import java.io.IOException; import java.io.StringReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import android.os.Bundle; import android.view.*; import android.view.View.*; import android.widget.*; public class MainActivity extends ActionBarActivity implements OnClickListener{ private Button btn_send = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_send = (Button) findViewById(R.id.btn_send); btn_send.setOnClickListener(this); } public void onClick(View v) { switch(v.getId()){ case R.id.btn_send: sendRequestWithHttpClient(); break; default: break; } } private void sendRequestWithHttpClient(){ new Thread(new Runnable(){ public void run() { try { HttpClient client = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://192.168.30.77:8000/static/css/text.xml"); HttpResponse response = client.execute(httpget); if(response.getStatusLine().getStatusCode() == 200){ HttpEntity entity = response.getEntity(); String response_txt = EntityUtils.toString(entity,"utf-8"); Log.d("response", "get response successed"); parseXMLwithPull(response_txt); } else{ Log.d("response", "get response failed"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } private void parseXMLwithPull(String xmldata){ XmlPullParserFactory factory; try { factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlpullparser = factory.newPullParser(); xmlpullparser.setInput(new StringReader(xmldata)); int eventType = xmlpullparser.getEventType(); String id = ""; String name = ""; String version = ""; while(eventType != xmlpullparser.END_DOCUMENT){ String nodeName = xmlpullparser.getName(); switch(eventType){ case XmlPullParser.START_TAG: if("id".equals(nodeName)){ try { id = xmlpullparser.nextText(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else if("name".equals(nodeName)){ try { name = xmlpullparser.nextText(); } catch (IOException e) { e.printStackTrace(); } }else if("version".equals(nodeName)){ try { version = xmlpullparser.nextText(); } catch (IOException e) { e.printStackTrace(); } } break; case XmlPullParser.END_TAG: if("app".equals(nodeName)){ Log.d("id = ", id); Log.d("name = ", name); Log.d("version = ", version); } break; } try { eventType = xmlpullparser.next(); } catch (IOException e) { e.printStackTrace(); } } } catch (XmlPullParserException e) { e.printStackTrace(); } } }
时间: 2024-10-12 02:56:32