类 : org.apache.http.client.HttpClient;

1. GET实现

Java代码
  1. packagecom.yarin.android.Examples_08_02;
  2. importjava.io.IOException;
  3. importorg.apache.http.HttpResponse;
  4. importorg.apache.http.HttpStatus;
  5. importorg.apache.http.client.ClientProtocolException;
  6. importorg.apache.http.client.HttpClient;
  7. importorg.apache.http.client.methods.HttpGet;
  8. importorg.apache.http.impl.client.DefaultHttpClient;
  9. importorg.apache.http.util.EntityUtils;
  10. importandroid.app.Activity;
  11. importandroid.content.Intent;
  12. importandroid.os.Bundle;
  13. importandroid.view.View;
  14. importandroid.widget.Button;
  15. importandroid.widget.TextView;
  16. publicclassActivity02extendsActivity{
  17. /**Calledwhentheactivityisfirstcreated.*/
  18. @Override
  19. publicvoidonCreate(BundlesavedInstanceState){
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.http);
  22. TextViewmTextView=(TextView)this.findViewById(R.id.TextView_HTTP);
  23. //http地址
  24. StringhttpUrl="http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
  25. //HttpGet连接对象
  26. HttpGethttpRequest=newHttpGet(httpUrl);
  27. try{
  28. //取得HttpClient对象
  29. HttpClienthttpclient=newDefaultHttpClient();
  30. //请求HttpClient,取得HttpResponse
  31. HttpResponsehttpResponse=httpclient.execute(httpRequest);
  32. //请求成功
  33. if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
  34. //取得返回的字符串
  35. StringstrResult=EntityUtils.toString(httpResponse
  36. .getEntity());
  37. mTextView.setText(strResult);
  38. }else{
  39. mTextView.setText("请求错误!");
  40. }
  41. }catch(ClientProtocolExceptione){
  42. mTextView.setText(e.getMessage().toString());
  43. }catch(IOExceptione){
  44. mTextView.setText(e.getMessage().toString());
  45. }catch(Exceptione){
  46. mTextView.setText(e.getMessage().toString());
  47. }
  48. //设置按键事件监听
  49. Buttonbutton_Back=(Button)findViewById(R.id.Button_Back);
  50. /*监听button的事件信息*/
  51. button_Back.setOnClickListener(newButton.OnClickListener(){
  52. publicvoidonClick(Viewv){
  53. /*新建一个Intent对象*/
  54. Intentintent=newIntent();
  55. /*指定intent要启动的类*/
  56. intent.setClass(Activity02.this,Activity01.class);
  57. /*启动一个新的Activity*/
  58. startActivity(intent);
  59. /*关闭当前的Activity*/
  60. Activity02.this.finish();
  61. }
  62. });
  63. }
  64. }

2. POST

Java代码
  1. packagecom.yarin.android.Examples_08_02;
  2. importjava.io.IOException;
  3. importjava.util.ArrayList;
  4. importjava.util.List;
  5. importorg.apache.http.HttpEntity;
  6. importorg.apache.http.HttpResponse;
  7. importorg.apache.http.HttpStatus;
  8. importorg.apache.http.NameValuePair;
  9. importorg.apache.http.client.ClientProtocolException;
  10. importorg.apache.http.client.HttpClient;
  11. importorg.apache.http.client.entity.UrlEncodedFormEntity;
  12. importorg.apache.http.client.methods.HttpPost;
  13. importorg.apache.http.impl.client.DefaultHttpClient;
  14. importorg.apache.http.message.BasicNameValuePair;
  15. importorg.apache.http.util.EntityUtils;
  16. importandroid.app.Activity;
  17. importandroid.content.Intent;
  18. importandroid.os.Bundle;
  19. importandroid.view.View;
  20. importandroid.widget.Button;
  21. importandroid.widget.TextView;
  22. publicclassActivity03extendsActivity{
  23. /**Calledwhentheactivityisfirstcreated.*/
  24. @Override
  25. publicvoidonCreate(BundlesavedInstanceState){
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.http);
  28. TextViewmTextView=(TextView)this.findViewById(R.id.TextView_HTTP);
  29. //http地址
  30. StringhttpUrl="http://192.168.1.110:8080/httpget.jsp";
  31. //HttpPost连接对象
  32. HttpPosthttpRequest=newHttpPost(httpUrl);
  33. //使用NameValuePair来保存要传递的Post参数
  34. List<NameValuePair>params=newArrayList<NameValuePair>();
  35. //添加要传递的参数
  36. params.add(newBasicNameValuePair("par","HttpClient_android_Post"));
  37. try{
  38. //设置字符集
  39. HttpEntityhttpentity=newUrlEncodedFormEntity(params,"gb2312");
  40. //请求httpRequest
  41. httpRequest.setEntity(httpentity);
  42. //取得默认的HttpClient
  43. HttpClienthttpclient=newDefaultHttpClient();
  44. //取得HttpResponse
  45. HttpResponsehttpResponse=httpclient.execute(httpRequest);
  46. //HttpStatus.SC_OK表示连接成功
  47. if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
  48. //取得返回的字符串
  49. StringstrResult=EntityUtils.toString(httpResponse.getEntity());
  50. mTextView.setText(strResult);
  51. }else{
  52. mTextView.setText("请求错误!");
  53. }
  54. }catch(ClientProtocolExceptione){
  55. mTextView.setText(e.getMessage().toString());
  56. }catch(IOExceptione){
  57. mTextView.setText(e.getMessage().toString());
  58. }catch(Exceptione){
  59. mTextView.setText(e.getMessage().toString());
  60. }
  61. //设置按键事件监听
  62. Buttonbutton_Back=(Button)findViewById(R.id.Button_Back);
  63. /*监听button的事件信息*/
  64. button_Back.setOnClickListener(newButton.OnClickListener(){
  65. publicvoidonClick(Viewv){
  66. /*新建一个Intent对象*/
  67. Intentintent=newIntent();
  68. /*指定intent要启动的类*/
  69. intent.setClass(Activity03.this,Activity01.class);
  70. /*启动一个新的Activity*/
  71. startActivity(intent);
  72. /*关闭当前的Activity*/
  73. Activity03.this.finish();
  74. }
  75. });
  76. }
  77. }

Apacheorg.apache.http.client.HttpClient;

更多相关文章

  1. 【开发工具】判断请求源是 手机 or PC
  2. android 打开文件
  3. Android(安卓)fragment中如何对listview添加监听事件
  4. Android(安卓)判断软键盘的状态(显示,隐藏)
  5. Android(安卓)Http网络开发神兵利器
  6. 根据请求头跳转判断Android&iOS
  7. android手势滑动——左右滑动效果实现
  8. Android(安卓)Socket网络通信
  9. android http请求并解析返回的xml

随机推荐

  1. 这是什么原因啊,没有找出错来啊(数据库用的
  2. mysql - 如果“a”==“b”回显“折扣”
  3. Windows下 MySQL更新密码
  4. Python:我如何从datetime.timedelta对象中
  5. Mysql 批量修改字段存储过程
  6. mysql连接数设置操作(Too many connection
  7. 如何以分布式方式将Zend_cache与memcache
  8. MySQL服务无法启动,1067
  9. 深入浅出MySQL阅读笔记-启动和关闭MySQL
  10. 在Codeigniter下,是否可能看到mysql_error