本文讲解Android进行网络访问及解析常用网络数据格式(XML,JSON )的方法。
WebView控件是Android提供给开发者用于将网页内容内嵌到自己的应用程序。
如通过WebView控件在应用程序中展示百度首页的示例代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
LinearLayout>
WebView webView=findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());//使得网页在当前Android应用程序中展示
webView.loadUrl("http://www.baidu.com");
<uses-permission android:name="android.permission.INTERNET"/>
获取数据的使用流程:
核心代码示例:
HttpURLConnection urlConnection;
InputStream inputStream;
BufferedReader bufferedReader;
//1. 获取HttpURLConnection对象
URL url = new URL("http://www.baidu.com");
urlConnection = (HttpURLConnection) url.openConnection();
//2. 设置访问网络相关参数
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(6000);
urlConnection.setReadTimeout(6000);
//3. 获取流数据
inputStream = urlConnection.getInputStream();
//4. 解析流数据
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String line="";
while ((line=bufferedReader.readLine())!=null)
{
response.append(line);
}
向数据库提交数据(post)与向数据库获取数据(get)类似,核心改动如下:
urlConnection.setRequestMethod("POST");
outputStream = urlConnection.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeBytes("usename=admin&&password=123")
OkHttp是一款开源的网络通信包。
//1. 实例化OkHttpClient
OkHttpClient okHttpClient=new OkHttpClient();
//2. 设置请求参数
Request request = new Request.Builder()
.url("http://baidu.com")
.build();
//3. 执行请求
Response response = okHttpClient.newCall(request).execute();
//4. 获取请求内容
String responseData = response.body().string();
//1. 实例化OkHttpClient
OkHttpClient okHttpClient=new OkHttpClient();
//2. 设置向服务器提交的数据
RequestBody requestBody = new FormBody.Builder()
.add("username", "mekeater")
.add("password", "123")
.build();
//3. 设置请求参数
Request request = new Request.Builder()
.url("http://baidu.com")
.post(requestBody)
.build();
//4. 执行请求
Response response = okHttpClient.newCall(request).execute();
//5. 获取请求内容
String responseData = response.body().string();
网络传输数据最常用的格式有两种分别是XML和JSON数据格式。本节先来讲讲如何解析XML格式的网络数据。
比如我们要解析的XML文件内容如下:
<apps>
<app>
<id>1id>
<name>Googlename>
<version>1.1version>
app>
<app>
<id>2id>
<name>Google plusname>
<version>2.1version>
app>
<app>
<id>3id>
<name>Google playname>
<version>3.0version>
app>
apps>
Pull方式主要是通过识别XML文件中节点的类型,来解析数据。我认为这个没什么好解释的,大家看一下示例代码就明白如何用Pull解析XML了。
private void parseXMLWithPull(String xmlData) {
try {
XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = pullParserFactory.newPullParser();
pullParser.setInput(new StringReader(xmlData));
int eventType = pullParser.getEventType();
String id="";
String name="";
String version="";
while (eventType!=XmlPullParser.END_DOCUMENT)//END_DOCUMENT代表xml文档读取完成
{
String nodeName=pullParser.getName();
switch (eventType)
{
case XmlPullParser.START_TAG://<...>这个标签开始的都是START_TAG
if ("id".equals(nodeName))
id=pullParser.nextText();
else if ("name".equals(nodeName))
name=pullParser.nextText();
else if ("version".equals(nodeName))
version=pullParser.nextText();
break;
case XmlPullParser.END_TAG://<.../>这个标签结尾的都是END_TAG
if ("app".equals(nodeName))
{
Log.d(TAG, "id = "+id);
Log.d(TAG, "name = "+name);
Log.d(TAG, "version = "+version);
}
break;
default:
break;
}
eventType=pullParser.next();//获取标签类型,是START_TAG还是END_TAG,或者是END_DOCUMENT
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
SAX解析方式是通过创建一个继承自DefaultHandler的类,然后重写startDocument,startElement,characters,endElement,endDocument,5个方法,这5个方法从名称上也很容易理解,SAX将不同的节点类型封装在不同的方法中,startDocument发生在开始解析XML文件时,startElement发生在开始解析某一个节点的时候,characters发生在获取解决节点内容的时候,endElement发生在解析一个节点完成的时候,endDocument发生在整个XML文档解析完成的时候,啰嗦这么多,其实看下示例代码就都懂了,上代码:
import android.util.Log;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ContentHandler extends DefaultHandler {
private static final String TAG = "ContentHandler";
private String nodeName;
private StringBuilder id;
private StringBuilder name;
private StringBuilder version;
@Override
public void startDocument() throws SAXException {
id=new StringBuilder();
name=new StringBuilder();
version=new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
//记录当前开始节点的名称
nodeName=localName;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
//匹配节点名称,读取节点内容
if (nodeName.equals("id"))
id.append(ch,start,length);
else if (nodeName.equals("name"))
name.append(ch,start,length);
else if (nodeName.equals("version"))
version.append(ch,start,length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
//根据结束节点名称,判断是否打印内容
if ("app".equals(localName))
{
Log.d(TAG, "id = "+id.toString().trim());
Log.d(TAG, "name = "+name.toString().trim());
Log.d(TAG, "version = "+version.toString().trim());
id.setLength(0);
name.setLength(0);
version.setLength(0);
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}
具体使用示例代码如下:
private void parseXMKWithSAX(String xmlData)
{
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
ContentHandler contentHandler = new ContentHandler();
xmlReader.setContentHandler(contentHandler);
xmlReader.parse(new InputSource(new StringReader(xmlData)));
}
catch (Exception e)
{
e.printStackTrace();
}
}
本节示例代码解析的JSON内容如下:
[{"id":"4","version":"4.0","name":"sun1"},
{"id":"5","version":"5.0","name":"sun2"},
{"id":"6","version":"6.0","name":"sun3"}]
JSONObject解析JSON文件非常简单,直接上示例代码:
private void parseJSONWithJSONObject(String jsonData)
{
try {
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");
Log.d(TAG, "id = "+id);
Log.d(TAG, "name = "+name);
Log.d(TAG, "version = "+version);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public class App {
private String id;
private String name;
private String version;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
private void parseJSONWithGSON(String jsonData)
{
Gson gson = new Gson();
List<App> appList = gson.fromJson(jsonData, new TypeToken<List<App>>() {
}.getType());
for (App app : appList) {
Log.d(TAG, "id = "+app.getId());
Log.d(TAG, "name = "+app.getName());
Log.d(TAG, "version = "+app.getVersion());
}
}
链接:https://pan.baidu.com/s/1aXtOQCXL6qzxEFLBlqXs1Q?pwd=n5ag
提取码:n5ag