网络基础:
- http方式访问网络
- okhttp方式访问网络
- pull方式解析xml数据
- SAX方式解析xml数据
5.JSONObject方式解析json数据
6.GSON方式解析数据
布局文件
<Button
android:id="@+id/send_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sendRequest"/>
<TextView
android:id="@+id/response_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
http方式访问网络
private void sendRequestWithHttp() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL("https://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(8000);
connection.setConnectTimeout(8000);
InputStream in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) !=null){
response.append(line);
}
showResponse(response.toString());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null){
connection.disconnect();
}
}
}
}).start();
}
okhttp方式
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://10.0.2.2./get_data.xml").build();
Response response = client.newCall(request).execute();
String data = response.body().string();
//showResponse(data);
//XMLWithPull(data);
//XMLWithSAX(data);
//JSONWithJSONObject(data);
JSONWithGson(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
解析xml数据pull方式
//解析XML文件pull方式
private void XMLWithPull(String data) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(data));
int eventType = parser.getEventType();
String id = "";
String name = "";
String version = "";
while(eventType != parser.END_DOCUMENT){
String nodeName = parser.getName();
switch (eventType){
case XmlPullParser.START_TAG:
if ("id".equals(nodeName))
id = parser.nextText();
if ("name".equals(nodeName))
name = parser.nextText();
if ("version".equals(nodeName))
version = parser.nextText();
break;
case XmlPullParser.END_TAG:
break;
default:
}
eventType = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
解析xml数据SAX方式
package com.example.bbw.networkdemo;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Created by bbw on 2017/7/23.
*/
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 endDocument() throws SAXException {
super.endDocument();
}
//开始解析某节点
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
nodeName = localName;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}
//获取节点中内容调用
@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);
}
}
//解析XML文件SAX方式
private void XMLWithSAX(String data) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLReader reader = factory.newSAXParser().getXMLReader();
ContentHandler handler = new ContentHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(new StringReader(data)));
} catch (Exception e) {
e.printStackTrace();
}
}
解析json数据jsonObject方式
//解析json文件jsonObject方式
private void JSONWithJSONObject(String data) {
try {
JSONArray array = new JSONArray(data);
for (int i=0;i<array.length();i++){
JSONObject jsonObject = array.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
解析json数据GSON方式
package com.example.bbw.networkdemo;
/**
* Created by bbw on 2017/7/23.
*/
public class bean {
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 JSONWithGson(String data) {
Gson gson = new Gson();
List<bean> beanList = gson.fromJson(data,new TypeToken<List<bean>>(){}.getType());
for (bean bean :beanList ){
String id = bean.getId();
String name = bean.getName();
String version = bean.getVersion();
}
}