HttpClient的get方式:

由于是网络请求,一定要放在子线程里做。

首先创建一个HttpClient对象:

HttpClient httpClient = new DefaultHttpClient();

然后创建一个HttpGet,将url地址传进去:

HttpGet httpGet = new HttpGet(path);

然后获取状态码,200的话就是访问成功了:

int code = response.getStatusLine().getStatusCode();

接下来得到响应:

HttpResponse response = httpClient.execute(httpPost);

再判断内容是否登入成功就行了。

完整的方法:

 1 public static void requestNewForGetLogin(final Handler handler, final String username, final String password) {
 2         
 3         new Thread(new Runnable() {
 4             
 5             @Override
 6             public void run() {
 7                 try {
 8                     String path = "http://192.168.0.106:8080/Starry/servlet/LoginServlet?username=" + URLEncoder.encode(username,"utf-8") + "&pwd=" + URLEncoder.encode(password,"utf-8");
 9                     //创建一个HttpClient对象
10                     HttpClient httpClient = new DefaultHttpClient();
11                     //设置请求方式
12                     HttpGet httpGet = new HttpGet(path);
13                     HttpResponse response = httpClient.execute(httpGet);
14                     int code = response.getStatusLine().getStatusCode();
15                     if(code == 200) {
16                         HttpEntity entity = response.getEntity();
17                         InputStream inputStream = entity.getContent();
18                         String result = StreamUtils.streamToString(inputStream);
19                         boolean issuccess = false;
20                         if(result.contains("success")){
21                             issuccess = true;
22                         }
23                         Message msg = Message.obtain();
24                         msg.obj = issuccess;
25                         msg.what = 1;
26                         handler.sendMessage(msg);
27                     }
28                 } catch (Exception e) {
29                     e.printStackTrace();
30                 }
31             }
32         }).start();
33     }
View Code

相关文章:

  • 2022-01-13
  • 2022-12-23
  • 2021-08-04
  • 2021-12-22
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2021-10-28
猜你喜欢
  • 2021-11-04
  • 2022-12-23
  • 2022-02-16
  • 2021-09-15
  • 2022-12-23
  • 2021-07-07
  • 2022-01-12
相关资源
相似解决方案