Android 的ListView 采用 MVC 的方式显示,Model 代表显示条目实体,View 代表 ListView, C 代表 Adapter。相比 iOS 中 Controller 和 View 相互融合的处境,Android 中的 MVC 更加清新自然。
Google 在 Android 中预制了很多示例 Demo ,集中放在了 ApiDemo 中,代码可以从 github 上单独下载。在 Mac 下的地址为 Library/Android/sdk/system-images/android-24/google_apis_playstore/x86/data/app/ApiDemos/ApiDemos.apk
主线程属于 UI 线程,被用来更新图像显示;子线程属于后台线程,可以用来处理网络请求、运行任务等。二者间通过 Handler 来进行联系。每一个
App 中都存在一个消息队列和循环对象(Looper)。Looper 就像一个敲钟人一样,定时检测新消息,将新消息顺序放在消息队列中。主线程会对消息队列中的消息逐条处理。不同 App 中的 Looper 通过 ThreadLocal 来区分。
数据传输格式
互联网通用的数据传输格式有 JSON 和 Xml 两种方式。Xml 用于重量级的数据交换;JSON 相比较轻量级。JSON 一般分为两种方式:
1 对象方式
{ key1:value1, key2:value2 ...}
2 数组格式
[value1, value2, value3 ...]
两者相互嵌套,能形成复杂的数据格式。
Android 中使用 JSONObject 类来处理 JSON 格式的数据。
JSONObject object = JSONObject(dataString);
JSONObject obj2 = object.getJSONObject("key1");
JSONArray arr3 = object.getJSONArray("key1");
两种常用网络请求
常用的网络请求方法为 POST 和 GET ,Android 中发送网络请求的接口封装在 URL 类中。下面是两个示例:
1 GET
public void onClick(View v){
String cityName = et_city.getText().toString();
try {
final String path = "http://wthrcdn.etouch.cn/weather_mini?city=" + URLEncoder.encode(cityName,"utf-8");
Thread t = new Thread(){
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = null;
try {
url = new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
int code = connection.getResponseCode();
if (code == 200){
InputStream in = null;
try {
in = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
String data = null;
try {
data = StreamTool.parseIn(in);
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONObject object = new JSONObject(data);
JSONObject weather = object.getJSONObject("data");
JSONArray forecast = weather.getJSONArray("forecast");
String res = forecast.getString(0);
Message msg = Message.obtain();
msg.what =Success;
msg.obj = res;
handler.sendMessage(msg);
in.close();
} catch (JSONException e) {
Message msg = Message.obtain();
msg.what = Failure;
handler.sendMessage(msg);
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
2 POST 请求
public void onClick1(View view) {
final String num = et_num.getText().toString().trim();
final String pwd = et_pwd.getText().toString().trim();
if (TextUtils.isEmpty(num) || TextUtils.isEmpty(pwd)){
Toast.makeText(LoginActivity.this, "号码或者密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
final String path = "http://192.168.128.157:8080/login?" + "name="+num+"&"+"password="+pwd;
new Thread(){
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Post 请求
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String data = "name="+ URLEncoder.encode(num, "utf-8") + "&password=" + URLEncoder.encode(pwd, "utf-8");
conn.setRequestProperty("Content-Length", + data.length() + "");
conn.setDoOutput(true);
conn.getOutputStream().write(data.getBytes());
int code = conn.getResponseCode();
if (code == 200) {
InputStream in = conn.getInputStream();
String res = StreamTool.parseIn(in);
Message msg = Message.obtain();
msg.obj = res;
msg.what = Success;
handler.sendMessage(msg);
}
} catch (IOException e) {
Message msg = Message.obtain();
msg.what = Failure;
handler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
网络请求还可以使用 apache 封装的对象方法和 okHttpClient 第三方开源框架。实际开发中,通常基于 okHttpClient , 对网络请求进行相应封装,但是其底层使用的方法都是 Url 类。掌握上述方法,能够对网络请求有更深刻的认识。
总结
本文简要介绍了网络上数据传输常用的两种格式:JSON 和 Xml ,并展示了 Android 中两种常见网络请求方式 GET 和 POST 的代码。希望能对小白入门有所帮助。
网友评论