WebView:在应用中嵌入一个浏览器
......
webView = (webView)findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true); //设置支持JS
webView.setWebViewClient(new WebViewClient()); //跳转网页时,不打开系统浏览器,而是在当前webView中显示
webView.loadUrl("http://www.baidu.com"); //传入网址
......
webView需要在AndroidManifest.xml中声明权限
HTTP协议
原理:客户端向服务端发送一条HTTP请求,服务器收到请求后返回数据给客户端,客户端再对数据进行解析和处理。
发送请求:HttpURLConnection和HttpClient两种方式;GET和POST两种方法。(GET从服务器获取数据,POST提交数据给服务器)
HttpURLConnection:
1、创建实例:new一个URL对象,并传入目标网络地址,调用openConnection()方法。
URl url = new URL("http://www.baidu.com");
HttpURLConnection connection = (HttpURLConnection)rul.openConnection();
2、设置方法:connection.setRequestMethod("GET");
3、其他设置:连接超时、读取超时、消息头等。
4、调用getInputStream()获取服务器返回的输入流,并读取:InputStream in = connection.getInputStream();
5、调用disconnect():connection.disconnect();
HttpClient(接口):
1、创建:HttpClient httpClient = new DefaultHttpClient();
2、设置方法:GET:
HttpGet httpGet = new HttpGet("http://www.baidu.com");
httpClient.execute(httpGet);
POST:
HttpPost httpPost = new HttpPost("http://www.baidu.com");
//创建NameValuePair结合存放待提交的参数
List<NameValuePair>parms = new ArrayList<NameValuePair>();
prams.add(new BasicNameValuePair("username","admin"));
prams.add(new BasicNameValuePair("password","123456"));
//传入entity
UrlEncodedFormEntity entity = new UrlEncodedEntity(parms,"utf-8");
httpPost.setEntity(entity);
httpClient.execute(httpPost);
3、返回HttoResponse对象,取出状态码:if(httpResponse.getStatusLine().getStatusCode()==200){......}
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity,"utf-8"); //含有中文需要指定字符集为utf-8
Message message = new Message();
meesage.what = SHOW_RESPONSE;
message.obj = response.toString();
handler.sendMessage(message);
}
解析xml:
1、Pull解析
......
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); //获得XmlPullParserFactory对象
XmlPullParser xmlPullParser = factory.newPullParser(); //获得XmlPullParser对象
xmlPullParser.setInput(new StringReader(xmlData)); //传入xml数据进行解析
......
2、Sax解析
新建一个类继承DefaultHandler,并重写5个父类方法:startDocument()、startElement()、characters()、endElement()、endDocument()
......
SAXParserFactory factory = SAXParserFactory.newInstance(); //获得SAXParserFactory对象
XMLReader xmlReader = factory.newSAXParser().getXMLReader(); //通过工厂获取XMLReader对象
ContentHandler handler = new ContentHandler();
xmlReader.setContentHandler(handler); //将handler对象设置到xmlReader中
xmlParser.parse(new InputSource(new StringReader(xmlData))); //调用parse()解析
......
解析JSON
体积小,省流量
1、JSONObject解析
......
JSONArray jsonArray = new JSONArray(jsonData); for(int i=0;i<jsonArray.length;i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version= jsonObject.getString("version");
}
......
2、GSON解析
需要下载一个gson的jar包放到libs目录下
将一段JSON格式的字符串自动映射成一个对象。
......
Gson gson = new Gson();
List<App> appList =gson.fromJson(jsonData,new TypeToken<List<App>>(){}.getType());
for(App app:appList){
......
}
......