get访问方式。
public void getCode2(View v){
// step1. 起线程new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
URL url = new URL("http://IP:8080/ems/getCode.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//step3. 设定网络访问的方式(GET/POST)
connection.setRequestMethod("GET");
connection.setDoInput(true);
//step4. 设定参数
//step5. 发起真正的请求,获得响应
connection.connect();
String value = connection.getHeaderField("Set-Cookie");
ID2=value.split(";")[0];
InputStream is = connection.getInputStream();
//step6. 解析响应内容
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 101, bitmap).sendToTarget();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
注册时post访问方法,带参数
public void regist2(View v){
//step1. 起线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
URL url = new URL("http://IP:8080/ems/regist.do");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//step3. 设定网络访问的方式(GET/POST)
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//step4. 设定参数
connection.connect();
OutputStream out = connection.getOutputStream();
PrintWriter pw = new PrintWriter(out);
String params="";
HashMap<String, String> map = new HashMap<String, String>();
map.put("loginname", "laozhang");
map.put("password", "654321");
map.put("realname", "zhang");
map.put("email", "zhang@bbc.com");
params = getParams(map);
pw.print(params);
pw.flush();
pw.close();
//step5. 发起真正的请求,获得响应
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
//step6. 解析响应内容
String result = br.readLine();
br.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 102, result).sendToTarget();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}