一个获得验证码的方法,Httpget对象。
public void getCode(View v){
//step1. 起线程new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
HttpClient client = new DefaultHttpClient();
//step3. 设定网络访问的方式(GET/POST)
HttpGet get = new HttpGet("http:// IP.141:8080/ems/getCode.do");
//step4. 设定参数
//step5. 发起真正的请求,获得响应
HttpResponse resp = client.execute(get);
//step6. 解析响应内容
Header header = resp.getFirstHeader("Set-Cookie");
String value = header.getValue();
ID = value.split(";")[0];
Log.d("TAG",ID);
HttpEntity entity = resp.getEntity();
InputStream is = entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 101, bitmap).sendToTarget();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
注册方法,Httppost对象访问方式
public void regist(View v){
//step1. 起线程
new Thread(){
public void run() {
try {
//step2. 创建能够进行网络访问的对象(HttpClient对象/HttpURLConnection对象)
HttpClient client = new DefaultHttpClient();
//step3. 设定网络访问的方式(GET/POST)
HttpPost post = new HttpPost("http://IP.141:8080/ems/regist.do");
//step4. 设定参数
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("loginname","laowang"));
parameters.add(new BasicNameValuePair("password","123456"));
parameters.add(new BasicNameValuePair("realname","wang"));
parameters.add(new BasicNameValuePair("email","wang@abc.com"));
HttpEntity entity = new UrlEncodedFormEntity(parameters);
post.setEntity(entity);
//step5. 发起真正的请求,获得响应
HttpResponse resp = client.execute(post);
//step6. 解析响应内容
HttpEntity respEntity = resp.getEntity();
InputStream in = respEntity.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String result = br.readLine();
br.close();
//step7. 将内容提交到主线程中显示
Message.obtain(handler, 102, result).sendToTarget();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();