單獨依靠內(nèi)置資源實現(xiàn)的App,在市場上不存在.通過用戶的操作,向服務(wù)器發(fā)送請求,并將服務(wù)器返回的數(shù)據(jù)在App頁面展現(xiàn)出來,是App運行的一般形式.App的正常運行就包括網(wǎng)絡(luò)請求,數(shù)據(jù)解析,以及在同一線程執(zhí)行任務(wù)時的回調(diào).
兩種網(wǎng)絡(luò)請求:HttpURLConnection
和OkHttp
Android向服務(wù)器發(fā)送請求的方式很多,總體分為兩類,一個是內(nèi)置的HttpURLConnection,另外是基于HttpURLConnection的開源框架.開源框架中又屬Square公司開源的OkHttp應(yīng)用范圍最廣.
連接網(wǎng)絡(luò)首先需要申請權(quán)限,在AndroidManifest.xml中添加:
<uses-permission android:name="android.permission.INTERNET"/>
獲取HttpUrlConnection,比較簡單,通過URL的openConnection方法可以創(chuàng)建.
private void getByUrlConnection(){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
try{
URL url = new URL("https://www.baidu.com");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setConnectTimeout(8000);
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(8000);
InputStream inputStream = urlConnection.getInputStream();
reader = new BufferedReader((new InputStreamReader(inputStream)));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null){
builder.append(line);
}
showResponse(builder.toString());
}catch (Exception e){
e.printStackTrace();
}finally {
if (reader != null){
try{
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
if (connection != null){
connection.disconnect();
}
}
}
}).start();
}
private void showResponse(final String response){
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(response);
}
});
}
用戶使用App期間,出現(xiàn)卡頓會嚴重影響用戶體驗,消耗時間的網(wǎng)絡(luò)請求需要放在子線程執(zhí)行.
也可以使用OkHttp發(fā)送請求:
private void getByOkHttp(){
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.baidu.com")
.get()
.build();
try{
Response response = client.newCall(request).execute();
String responseData = response.body().string();
showResponse(responseData);
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
數(shù)據(jù)請求中的兩種形式:XML和Json
網(wǎng)絡(luò)請求的數(shù)據(jù)需要按照一定的規(guī)則和協(xié)議,其中XML和Json兩種數(shù)據(jù)形式,因為體積小,規(guī)則簡單和實現(xiàn)方便得到了廣泛的使用.
XML是Extensible Markup Language的簡稱,幾乎可以在任何程序中進行數(shù)據(jù)交換.下面是一種簡單的形式:
<apps>
<app>
<id>1</id>
<name>Chrome</name>
<version>1.0</version>
</app>
<app>
<id>2</id>
<name>Safari</name>
<version>1.0</version>
</app>
<app>
<id>3</id>
<name>FireFox</name>
<version>1.0</version>
</app>
</apps>
Android對XML的解析方式有Pull和SAX兩種.它們本質(zhì)都是對文件按行讀取,按照元素標(biāo)記的開頭和結(jié)尾來獲取元素的內(nèi)容.
Pull解析XML
private void pullXML(String data){
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
pullParser.setInput(new StringReader(data));
int eventType = pullParser.getEventType();
String id = "";
String name = "";
String version = "";
while (eventType != XmlPullParser.END_DOCUMENT){
String nodeName = pullParser.getName();
switch (eventType){
case XmlPullParser.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:{
if ("app".equals(nodeName)){
Log.e("END_TAG", "id = " + id);
Log.e("END_TAG", "name = " + name);
Log.e("END_TAG", "version = " + version);
}
}
}
eventType = pullParser.next();
}
}catch (Exception e){
e.printStackTrace();
}
}
SAX解析XML
public class ContentHandler extends DefaultHandler {
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 {
Log.e("startElement", "startElement: " + uri + localName + qName + attributes.toString());
nodeName = localName;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if ("id".equals(nodeName)){
id.append(ch, start, length);
}else if ("name".equals(nodeName)){
name.append(ch, start, length);
}else if ("version".equals(nodeName)){
version.append(ch, start, length);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("app".equals(localName)){
Log.e("endElement", "id = " + id);
Log.e("endElement", "name = " + name);
Log.e("endElement", "version = " + version);
id.setLength(0);
name.setLength(0);
version.setLength(0);
}
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}
private void parseXmlUseSAX(String data){
SAXParserFactory factory = SAXParserFactory.newInstance();
try{
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
ContentHandler contentH = new ContentHandler();
xmlReader.setContentHandler(contentH);
xmlReader.parse(new InputSource(new StringReader(data)));
}catch (Exception e){
e.printStackTrace();
}
}
Json是JavaScript Object Notation的簡稱,具有體積小,消耗流量少的特點,誕生于重視流量的移動端時代,二者的結(jié)合,讓移動端更顯流暢.Android系統(tǒng)包中自帶了Json的的解析方式-JsonObject.Google開源的GSON解析Json也很方便.
JsonObject解析Json
private void parseJsonUseJsonObject(String data){
try{
JSONArray jsonArray = new JSONArray(data);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsObj = jsonArray.getJSONObject(i);
String id = jsObj.getString("id");
String name = jsObj.getString("name");
String version = jsObj.getString("version");
Log.e("parseJsonUseJsonObject", "parseJsonUseJsonObject: " + id + name + version);
}
}catch (Exception e){
e.printStackTrace();
}
}
GSON可以很方便地將Json格式的數(shù)據(jù)映射成對象,也即時MVC編程思想中的Model.
GSON解析Json
public class App {
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 String id;
private String name;
private String version;
}
private void parseJsonUseGson(String jsonData){
Gson gson = new Gson();
List<App> appList = gson.fromJson(jsonData, new TypeToken<List<App>>(){}.getType());
for (App app: appList){
Log.e("App", "id: " + app.getId());
Log.e("App", "name: " + app.getName());
Log.e("App", "version: " + app.getVersion());
}
}
數(shù)據(jù)回調(diào)
App為了提高運行效率和更好的用戶體驗,使用了多線程技術(shù),可以同時處理多個任務(wù).比如常常用到的網(wǎng)絡(luò)請求,就是在子線程中執(zhí)行.子線程中的執(zhí)行結(jié)果,可以通過數(shù)據(jù)回調(diào)的方式順利實現(xiàn)任務(wù)的按序執(zhí)行.
實現(xiàn)數(shù)據(jù)回調(diào),首先需要定義接口,然后在子線程中使用.
//定義接口
public interface HttpCallbackListener {
void onFinish(String response);
void onError(Exception e);
}
//使用
public static void SendHttpRequest(final String address, final HttpCallbackListener listener){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try{
URL url = new URL(address);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null){
response.append(line);
}
if (listener != null){
listener.onFinish(response.toString());
}
}catch (Exception e){
if (listener != null){
listener.onError(e);
}
}finally {
if (connection != null){
connection.disconnect();
}
}
}
}).start();
}
//調(diào)用
HttpUtil.SendHttpRequest("http://10.0.2.2/get_data.json", new HttpCallbackListener() {
@Override
public void onFinish(String response) {
parseJson(response);
}
@Override
public void onError(Exception e) {
}
});
總結(jié)
網(wǎng)絡(luò)相關(guān)的知識,可以用3個二和1個一來概括:
兩種網(wǎng)絡(luò)請求方式HttpURLConnection和OkHttp;
兩種數(shù)據(jù)傳遞方式XML和Json,其中XML的解析用Pull和SAX,Json的解析用JsonObject和GSON;
一種數(shù)據(jù)回調(diào),通過接口(Interface)來實現(xiàn).
喜歡和關(guān)注都是對我的鼓勵和支持~