Android 系列笔记 四

handler 消息处理器:用于发送、接收消息

主线程中更新UI

1
2
3
4
5
6
7
8
9
10
11
Handler handler=new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case SUCCESS:
break;
default:
break
}
}
};

子线程向消息池发送消息

1
2
3
4
5
6
7
8
new Thread(new Runnable(){
public void run(){
Message msg=new Message();
msg.what=0;
msg.obj=要操作的变量;
handler.sendMessage(msg);
}
}).start();

get请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
HttpURLConnection conn = null;
try {
Log.i("getPicture", address);
URL url = new URL(address);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);//设置请求超时时间
conn.setReadTimeout(5000);//设置读取超时时间
conn.setRequestMethod("GET");//设置请求类型
conn.connect();

int responseCode = conn.getResponseCode();//获取请求响应码
if (responseCode == 200) {//请求成功
Log.i("get picture", "loading......");
Bitmap bitmap = BitmapFactory.decodeStream(conn.getInputStream());
if(bitmap!=null) {
Message msg = new Message();//向主线程发送一条消息
msg.what = SUCCESS;
msg.obj = bitmap;
handler.sendMessage(msg);
}else{
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
}
} else {
Log.i("error", "visit wrong");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.disconnect();
}

图片缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
String urlString = "";
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(10000);//设置请求超时时间
conn.setReadTimeout(5000);//设置读取超时时间
conn.setRequestMethod("GET");//设置请求类型
conn.connect();
InputStream is = connection.getInputStream();
byte[] buffer = new byte[1024];
File file = new File(getCacheDir(), "");
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
while((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
}
fos.close();

post请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
new Thread(new Runnable(){
public void run(){
//执行网络操作
runOnUiThread(new Runnable(){
public void run(){
//就是在主线程中执行操作
Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
HttpURLConnection conn = null;
try {
String address = "www.baidu.com";
URL url = new URL(address);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);//设置请求超时时间
conn.setReadTimeout(5000);//设置读取超时时间
conn.setRequestMethod("POST");//设置请求类型

String text = "username=" + name + "&password=" + password;
//设置post请求属性
conn.setRequestProperty("Content-Length", text.length() + "");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(text.getBytes());
conn.connect();

int responseCode = conn.getResponseCode();//获取请求响应码
if (responseCode == 200) {//请求成功
InputStream is = conn.getInputStream();
String info = Utils.getTextFromStream(is);
} else {
Log.i("error", "visit wrong");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.disconnect();
}
}
});
}
});

编码问题

将汉字转为以%开头的字符串

1
URLEncoder.encode(editText.getString(), "utf-8");

采用iso8859-1编码对姓名进行逆转,转成字节数组,再使用utf-8对数据编码

1
username=new String(username.getBytes("iso8859-1"),"utf-8");