《安卓网络编程》之第三篇 使用Apache接口
在Android系统中,提供了一下三种通信接口:
- 标准的Java 接口:java.net
- Apache接口:org.apache.http
- Android网络接口:android.net.http
在android系统中,包含了Apache HttpClient库,此库为执行Android中的网络操作之首选方法。
Apache应用基础
联网流程
在Android系统中,可以采用HttpPost和HttpGet来封装Post请求和Get请求,然后再使用HttpClient的excute()方法发送Post或者get请求来返回服务器的响应数据。使用Apache联网的基本流程如下:
- 设置连接和读取超时时间,并建立HttpClient对象
- 实现Get请求
- 实现Post发送请求处理
- 使用Response响应请求
这样,使用Apache实现联网处理数据交互的过程就完成了,无论多么复杂的项目,都必须遵循上面的流程。
下面是一个运行在安卓上的工程实例:
1 package irdc.httpSHI; 2 3 4 /*必需引用apache.http相关类来建立HTTP联机*/ 5 import org.apache.http.HttpResponse; 6 import org.apache.http.NameValuePair; 7 import org.apache.http.client.ClientProtocolException; 8 import org.apache.http.client.entity.UrlEncodedFormEntity; 9 import org.apache.http.client.methods.HttpGet; 10 import org.apache.http.client.methods.HttpPost; 11 import org.apache.http.impl.client.DefaultHttpClient; 12 import org.apache.http.message.BasicNameValuePair; 13 import org.apache.http.protocol.HTTP; 14 import org.apache.http.util.EntityUtils; 15 /*必需引用java.io 与java.util相关类?来读写档案*/ 16 import irdc.httpSHI.R; 17 18 import java.io.IOException; 19 import java.util.ArrayList; 20 import java.util.List; 21 import java.util.regex.Matcher; 22 import java.util.regex.Pattern; 23 24 import android.app.Activity; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.widget.Button; 28 import android.widget.TextView; 29 30 public class httpSHI extends Activity 31 { 32 /*宣╳两个Button物件,与几个TextView物件*/ 33 private Button mButton1,mButton2; 34 private TextView mTextView1; 35 36 /** Called when the activity is first created. */ 37 @Override 38 public void onCreate(Bundle savedInstanceState) 39 { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.main); 42 43 /*透过findViewById建构巳建立TextView与Button对象*/ 44 mButton1 =(Button) findViewById(R.id.myButton1); 45 mButton2 =(Button) findViewById(R.id.myButton2); 46 mTextView1 = (TextView) findViewById(R.id.myTextView1); 47 48 mButton1.setOnClickListener(new Button.OnClickListener() 49 { 50 51 @Override 52 public void onClick(View v) 53 { 54 55 String uriAPI = "http://www.baidu.com"; 56 /*建立HTTP Post联机*/ 57 HttpPost httpRequest = new HttpPost(uriAPI); 58 /* 59 * Post传送变量必须用NameValuePair[]存储 60 */ 61 List <NameValuePair> params = new ArrayList <NameValuePair>(); 62 params.add(new BasicNameValuePair("str", "I am Post String")); 63 try 64 { 65 /*发叨HTTP request*/ 66 httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 67 /*取得HTTP response*/ 68 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 69 /*若状态码为200*/ 70 if(httpResponse.getStatusLine().getStatusCode() == 200) 71 { 72 /*获取字符串*/ 73 String strResult = EntityUtils.toString(httpResponse.getEntity()); 74 mTextView1.setText(strResult); 75 } 76 else 77 { 78 mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString()); 79 } 80 } 81 catch (ClientProtocolException e) 82 { 83 mTextView1.setText(e.getMessage().toString()); 84 e.printStackTrace(); 85 } 86 catch (IOException e) 87 { 88 mTextView1.setText(e.getMessage().toString()); 89 e.printStackTrace(); 90 } 91 catch (Exception e) 92 { 93 mTextView1.setText(e.getMessage().toString()); 94 e.printStackTrace(); 95 } 96 97 } 98 }); 99 mButton2.setOnClickListener(new Button.OnClickListener() 100 { 101 @Override 102 public void onClick(View v) 103 { 104 // TODO Auto-generated method stub 105 String uriAPI = "http://www.baidu.com/str=I+am+Get+String"; 106 /*建立HTTP Get联机*/ 107 HttpGet httpRequest = new HttpGet(uriAPI); 108 try 109 { 110 /*发送获取的HTTP request*/ 111 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 112 /*若状态码为200*/ 113 if(httpResponse.getStatusLine().getStatusCode() == 200) 114 { 115 /*取叨并应?串*/ 116 String strResult = EntityUtils.toString(httpResponse.getEntity()); 117 strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult); 118 mTextView1.setText(strResult); 119 } 120 else 121 { 122 mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString()); 123 } 124 } 125 catch (ClientProtocolException e) 126 { 127 mTextView1.setText(e.getMessage().toString()); 128 e.printStackTrace(); 129 } 130 catch (IOException e) 131 { 132 mTextView1.setText(e.getMessage().toString()); 133 e.printStackTrace(); 134 } 135 catch (Exception e) 136 { 137 mTextView1.setText(e.getMessage().toString()); 138 e.printStackTrace(); 139 } 140 } 141 }); 142 } 143 public String eregi_replace(String strFrom, String strTo, String strTarget) 144 { 145 String strPattern = "(?i)"+strFrom; 146 Pattern p = Pattern.compile(strPattern); 147 Matcher m = p.matcher(strTarget); 148 if(m.find()) 149 { 150 return strTarget.replaceAll(strFrom, strTo); 151 } 152 else 153 { 154 return strTarget; 155 } 156 } 157 }
实现了post和get俩种方式联网。
xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/white" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title"/> <Button android:id="@+id/myButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_button1" /> <Button android:id="@+id/myButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str_button2" /> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。