HttpClient请求网络 xml解析
2015-11-20 16:34
337 查看
xml解析包含dom解析、sax解析、pull解析,这里给出pull解析的例子。
package com.example.myweathertest; 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.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements OnClickListener { private Button mButtonRequest; private TextView mTextView; String city ; String weather; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonRequest = (Button) findViewById(R.id.button_request); mTextView = (TextView) findViewById(R.id.textview); mButtonRequest.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button_request: sendRequestWithHttpClient(); mTextView.setText(city); break; default: break; } } //新开一个线程请求网络,用HttpClient public void sendRequestWithHttpClient(){ new Thread(new Runnable() { @Override public void run() { try { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet("http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0"); HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode() == 200){ HttpEntity entity = httpResponse.getEntity(); String response = EntityUtils.toString(entity,"utf-8"); parseXMLWithPull(response); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } //解析得到的数据 private void parseXMLWithPull(String xmlData){ try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(new StringReader(xmlData)); int eventType = xmlPullParser.getEventType(); city = ""; weather = ""; while(eventType != XmlPullParser.END_DOCUMENT){ String nodeName = xmlPullParser.getName(); switch (eventType) { //开始解析某个节点 case XmlPullParser.START_TAG: if("city".equals(nodeName)){ city = xmlPullParser.nextText(); }else if("weather".equals(nodeName)){ weather = xmlPullParser.nextText(); } break; case XmlPullParser.END_TAG: if("app".equals(nodeName)){ Log.d("MainActivity","city is" + city); Log.d("MainActivity","weather is" + weather); } break; default: break; } eventType = xmlPullParser.next(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- 想深入了解TCP机制和相关参数优化吗(下)
- iOS xcode 7网络请求错误failure Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded
- 网络错误定位案例 ICMP host *** unreachable - admin prohibited
- Android-HTTP实例——使用GET方法、POST方法发送请求(安卓4.0以下版本)
- HTTP协议的8种请求类型介绍
- 从TCP协议的原理来谈谈rst复位攻击
- 网络通信
- 基于Nutch+Hadoop+Hbase+ElasticSearch的网络爬虫及搜索引擎
- AFNetWorking初探之AFHTTPRequestOperation(二)
- tcp 出现rst情况整理
- socket网络编程中read与recv区别
- android网络编程之——客户端上传信息的代码
- Nginx配置https访问
- 【转】Jmeter Http请求界面解释
- 虚拟机ping不通主机
- 关于 Xcode7网络请求的Http 协议改为 Https 协议的程序修改
- HTTP客户端服务端TCP连接内幕
- TCP连接探测中的Keepalive和心跳包
- ab和jmeter进行GET/POST压力测试的使用心得和比较
- [python&php 网络编程]设定并获取socket超时时间