zoukankan      html  css  js  c++  java
  • httpclient实例

      1 

    在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端。它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET和HTTP POST调用。 HTTP工作原理: 
    1.客户端(一般是指浏览器,这里是指自己写的程序)与服务器建立连接 2.建立连接后,客户端向服务器发送请求 3.服务器接收到请求后,向客户端发送响应信息 4.客户端与服务器断开连接 HttpClient的一般使用步骤: 
    1.使用DefaultHttpClient类实例化HttpClient对象 
    2.创建HttpGet或HttpPost对象,将要请求的URL通过构造方法传入HttpGet或HttpPost对象。 
    3.调用execute方法发送HTTP GET或HTTP POST请求,并返回HttpResponse对象。 4.通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理。 最后记得要在AndroidManifest.xml文件添加网络权限 
    <uses-permission android:name="android.permission.INTERNET" /> 


    这里有一个非常棒的http通讯的总结,我看了以后茅塞顿开。 2 3 先贴代码: 4 5 01 6 public class Activity1 extends Activity { 7 02 8 9 03 10 private final String DEBUG_TAG = "System.out"; 11 04 12 13 05 14 private TextView mTextView; 15 06 16 private Button mButton; 17 07 18 19 08 20 protected void onCreate(Bundle savedInstanceState) { 21 09 22 super.onCreate(savedInstanceState); 23 10 24 setContentView(R.layout.main); 25 11 26 27 12 28 mTextView = (TextView) findViewById(R.id.TextView01); 29 13 30 mButton = (Button) findViewById(R.id.Button01); 31 14 32 mButton.setOnClickListener(new httpListener()); 33 15 34 } 35 16 36 37 17 38 // 设置按钮监听器 39 18 40 class httpListener implements OnClickListener { 41 19 42 public void onClick(View v) { 43 20 44 refresh(); 45 21 46 } 47 22 48 } 49 23 50 51 24 52 private void refresh() { 53 25 54 String httpUrl = "http://192.168.0.101:8080/Test/test.jsp"; 55 26 56 // URL可以加参数 57 27 58 // String httpUrl = 59 28 60 // "http://192.168.0.101:8080/Test/test.jsp?par=abcdefg"; 61 29 62 String resultData = ""; 63 30 64 URL url = null; 65 31 66 try { 67 32 68 // 创建一个URL对象 69 33 70 url = new URL(httpUrl); 71 34 72 } catch (MalformedURLException e) { 73 35 74 Log.d(DEBUG_TAG, "create URL Exception"); 75 36 76 } 77 37 78 // 声明HttpURLConnection对象 79 38 80 HttpURLConnection urlConn = null; 81 39 82 // 声明InputStreamReader对象 83 40 84 InputStreamReader in = null; 85 41 86 // 声明BufferedReader对象 87 42 88 BufferedReader buffer = null; 89 43 90 String inputLine = null; 91 44 92 if (url != null) { 93 45 94 try { 95 46 96 // 使用HttpURLConnection打开连接 97 47 98 urlConn = (HttpURLConnection) url.openConnection(); 99 48 100 // 得到读取的内容(流) 101 49 102 in = new InputStreamReader(urlConn.getInputStream()); 103 50 104 // 创建BufferReader对象,输出时候用到 105 51 106 buffer = new BufferedReader(in); 107 52 108 // 使用循环来读取数据 109 53 110 while ((inputLine = buffer.readLine()) != null) { 111 54 112 // 在每一行后面加上换行 113 55 114 resultData += inputLine + " "; 115 56 116 } 117 57 118 // 设置显示取的的内容 119 58 120 if (resultData != null && !resultData.equals("")) { 121 59 122 mTextView.setText(resultData); 123 60 124 } else { 125 61 126 mTextView.setText("读取的内容为空"); 127 62 128 } 129 63 130 } catch (IOException e) { 131 64 132 e.printStackTrace(); 133 65 134 } finally { 135 66 136 try { 137 67 138 // 关闭InputStreamReader 139 68 140 in.close(); 141 69 142 // 关闭URL连接 143 70 144 urlConn.disconnect(); 145 71 146 } catch (IOException e) { 147 72 148 e.printStackTrace(); 149 73 150 } 151 74 152 } 153 75 154 } else { 155 76 156 Log.d(DEBUG_TAG, "URL is NULL"); 157 77 158 } 159 78 160 } 161 79 162 } 163 164 165 166 第二种方式: 167 168 001 169 public class Activity2 extends Activity { 170 002 171 172 003 173 private final String DEBUG_TAG = "System.out"; 174 004 175 176 005 177 private TextView mTextView; 178 006 179 private Button mButton; 180 007 181 182 008 183 protected void onCreate(Bundle savedInstanceState) { 184 009 185 super.onCreate(savedInstanceState); 186 010 187 setContentView(R.layout.main); 188 011 189 190 012 191 mTextView = (TextView) findViewById(R.id.TextView01); 192 013 193 mButton = (Button) findViewById(R.id.Button01); 194 014 195 mButton.setOnClickListener(new httpListener()); 196 015 197 } 198 016 199 200 017 201 // 设置按钮监听器 202 018 203 class httpListener implements OnClickListener { 204 019 205 public void onClick(View v) { 206 020 207 refresh(); 208 021 209 } 210 022 211 } 212 023 213 214 024 215 private void refresh() { 216 025 217 String httpUrl = "http://192.168.0.101:8080/Test/test.jsp"; 218 026 219 String resultData = ""; 220 027 221 URL url = null; 222 028 223 try { 224 029 225 // 创建一个URL对象 226 030 227 url = new URL(httpUrl); 228 031 229 } catch (MalformedURLException e) { 230 032 231 Log.d(DEBUG_TAG, "create URL Exception"); 232 033 233 } 234 034 235 // 声明HttpURLConnection对象 236 035 237 HttpURLConnection urlConn = null; 238 036 239 // 声明InputStreamReader对象 240 037 241 InputStreamReader in = null; 242 038 243 // 声明BufferedReader对象 244 039 245 BufferedReader buffer = null; 246 040 247 String inputLine = null; 248 041 249 // 声明DataOutputStream流 250 042 251 DataOutputStream out = null; 252 043 253 if (url != null) { 254 044 255 try { 256 045 257 // 使用HttpURLConnection打开连接 258 046 259 urlConn = (HttpURLConnection) url.openConnection(); 260 047 261 // 因为这个是POST请求所以要设置为true 262 048 263 urlConn.setDoInput(true); 264 049 265 urlConn.setDoOutput(true); 266 050 267 // 设置POST方式 268 051 269 urlConn.setRequestMethod("POST"); 270 052 271 // POST请求不能设置缓存 272 053 273 urlConn.setUseCaches(false); 274 054 275 urlConn.setInstanceFollowRedirects(false); 276 055 277 // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的 278 056 279 urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 280 057 281 // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成 282 058 283 // 要注意的是connectio.getOutputStream会隐含的进行connect 284 059 285 urlConn.connect(); 286 060 287 // DataOutputStream流 288 061 289 out = new DataOutputStream(urlConn.getOutputStream()); 290 062 291 String content = "par=" + URLEncoder.encode("abcdefg", "gb2312"); 292 063 293 // 将要上传的内容写入流中 294 064 295 out.writeBytes(content); 296 065 297 // 得到读取的内容(流) 298 066 299 in = new InputStreamReader(urlConn.getInputStream()); 300 067 301 // 创建BufferReader对象,输出时候用到 302 068 303 buffer = new BufferedReader(in); 304 069 305 // 使用循环来读取数据 306 070 307 while ((inputLine = buffer.readLine()) != null) { 308 071 309 // 在每一行后面加上换行 310 072 311 resultData += inputLine + " "; 312 073 313 } 314 074 315 // 设置显示取的的内容 316 075 317 if (resultData != null && !resultData.equals("")) { 318 076 319 mTextView.setText(resultData); 320 077 321 } else { 322 078 323 mTextView.setText("读取的内容为空"); 324 079 325 } 326 080 327 } catch (IOException e) { 328 081 329 e.printStackTrace(); 330 082 331 } finally { 332 083 333 try { 334 084 335 // 刷新DataOutputStream流 336 085 337 out.flush(); 338 086 339 // 关闭DataOutputStream流 340 087 341 out.close(); 342 088 343 // 关闭InputStreamReader 344 089 345 in.close(); 346 090 347 // 关闭URL连接 348 091 349 urlConn.disconnect(); 350 092 351 } catch (IOException e) { 352 093 353 e.printStackTrace(); 354 094 355 } 356 095 357 } 358 096 359 } else { 360 097 361 Log.d(DEBUG_TAG, "URL is NULL"); 362 098 363 } 364 099 365 } 366 100 367 } 368 369 370 第三种方式 371 372 01 373 public class Activity3 extends Activity{ 374 02 375 private TextView mTextView; 376 03 377 private Button mButton; 378 04 379 380 05 381 protected void onCreate(Bundle savedInstanceState) { 382 06 383 super.onCreate(savedInstanceState); 384 07 385 setContentView(R.layout.main); 386 08 387 mTextView = (TextView) findViewById(R.id.TextView01); 388 09 389 mButton = (Button) findViewById(R.id.Button01); 390 10 391 mButton.setOnClickListener(new httpListener()); 392 11 393 } 394 12 395 396 13 397 // 设置按钮监听器 398 14 399 class httpListener implements OnClickListener { 400 15 401 public void onClick(View v) { 402 16 403 String httpUrl = "http://192.168.0.101:8080/Test/test.jsp?par=HttpClient_android_Get"; 404 17 405 // HttpGet连接对象 406 18 407 HttpGet httpRequest = new HttpGet(httpUrl); 408 19 409 try { 410 20 411 // 取的HttpClient对象 412 21 413 HttpClient httpclient = new DefaultHttpClient(); 414 22 415 // 请求HttpClient,取的HttpResponse 416 23 417 HttpResponse httpResponse = httpclient.execute(httpRequest); 418 24 419 // 请求成功 420 25 421 if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 422 26 423 // 取的返回的字符串 424 27 425 String strResult = EntityUtils.toString(httpResponse.getEntity()); 426 28 427 // 这个返回值可能会在行尾出现小方格 428 29 429 // 在TextView要显示的文字过滤掉回车符(" ")就可以正常显示了。 430 30 431 String strsResult = strResult.replace(" ", ""); 432 31 433 mTextView.setText(strsResult); 434 32 435 } else { 436 33 437 mTextView.setText("请求错误"); 438 34 439 } 440 35 441 } catch (ClientProtocolException e) { 442 36 443 mTextView.setText(e.getMessage().toString()); 444 37 445 } catch (IOException e) { 446 38 447 mTextView.setText(e.getMessage().toString()); 448 39 449 } catch (Exception e) { 450 40 451 mTextView.setText(e.getMessage().toString()); 452 41 453 } 454 42 455 } 456 43 457 } 458 44 459 } 460 461 462 第四种方式 463 464 01 465 public class Activity4 extends Activity{ 466 02 467 private TextView mTextView; 468 03 469 private Button mButton; 470 04 471 472 05 473 protected void onCreate(Bundle savedInstanceState) { 474 06 475 super.onCreate(savedInstanceState); 476 07 477 setContentView(R.layout.main); 478 08 479 mTextView = (TextView) findViewById(R.id.TextView01); 480 09 481 mButton = (Button) findViewById(R.id.Button01); 482 10 483 mButton.setOnClickListener(new httpListener()); 484 11 485 } 486 12 487 488 13 489 // 设置按钮监听器 490 14 491 class httpListener implements OnClickListener { 492 15 493 public void onClick(View arg0) { 494 16 495 String httpUrl = "http://192.168.0.101:8080/Test/test.jsp"; 496 17 497 // 创建HttpPost连接对象 498 18 499 HttpPost httpRequest = new HttpPost(httpUrl); 500 19 501 // 使用NameValuePair来保存要传递的Post参数 502 20 503 List params = new ArrayList(); 504 21 505 // 添加要传递的参数 506 22 507 params.add(new BasicNameValuePair("par","HttpClient_android_Post")); 508 23 509 try { 510 24 511 // 设置字符集 512 25 513 HttpEntity httpentity = new UrlEncodedFormEntity(params,"gb2312"); 514 26 515 // 请求httpRequest 516 27 517 httpRequest.setEntity(httpentity); 518 28 519 // 取的默认的HttpClient 520 29 521 HttpClient httpclient = new DefaultHttpClient(); 522 30 523 // 取的HttpResponse 524 31 525 HttpResponse httpResponse = httpclient.execute(httpRequest); 526 32 527 // HttpStatus.SC_OK表示连接成功 528 33 529 if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 530 34 531 // 取的返回的字符串 532 35 533 String strResult = EntityUtils.toString(httpResponse.getEntity()); 534 36 535 // 这个返回值可能会在行尾出现小方格 536 37 537 // 在TextView要显示的文字过滤掉回车符(" ")就可以正常显示了。 538 38 539 String strsResult = strResult.replace(" ", ""); 540 39 541 mTextView.setText(strsResult); 542 40 543 } else { 544 41 545 mTextView.setText("请求错误"); 546 42 547 } 548 43 549 } catch (ClientProtocolException e) { 550 44 551 mTextView.setText(e.getMessage().toString()); 552 45 553 } catch (IOException e) { 554 46 555 mTextView.setText(e.getMessage().toString()); 556 47 557 } catch (Exception e) { 558 48 559 mTextView.setText(e.getMessage().toString()); 560 49 561 } 562 50 563 } 564 51 565 } 566 52 567 }

    文章出处:1、http://blog.csdn.net/jdsjlzx/article/details/6876958 2、http://wenku.baidu.com/link?url=1IuuCu1wvwVjpcRZQ8WN0--lDTzazCXM0CMTpZ84ROI9acdPpQiQ-yXL9JqpSjsDegGHDGbiVgkfZOR5KhNuFEeH4ZuWGha1ch9CVrCJjkW

    Axepudding Contact:axepudding@qq.com
  • 相关阅读:
    Sky
    MyEclipse 10中文汉化教程
    算法
    查找众数
    格雷码算法
    commons-email
    java
    IO端寻址
    存储器
    汇编顺序程序设计
  • 原文地址:https://www.cnblogs.com/axepudding/p/4754321.html
Copyright © 2011-2022 走看看