Android 网络通信 HTTP
摘要
1. Http GET 方法访问网站
2. Http POST访问网站
3. HttpClient进行Get方式通信
4. HttpClient进行Post方式通信
--------------------------------------------------------
工程代码:
--------------------------------------------------------
1. Http GET 方法访问网站
使用Get方式与网络通信是最常见的Http通信,建立链接之后就可以通过输入流读取网络数据。
new AsyncTask<String, Void, Void>() { @Override protected Void doInBackground(String... params) { // TODO Auto-generated method stub try { URL url = new URL(params[0]); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { Log.d("CARLOZ", line); } br.close(); isr.close(); is.close(); is = conn.getInputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }.execute("http://carloz.duapp.com");
运行结果
08-25 12:56:24.160: D/CARLOZ(27942): <!DOCTYPE html> 08-25 12:56:24.160: D/CARLOZ(27942): <html> 08-25 12:56:24.160: D/CARLOZ(27942): <head> 08-25 12:56:24.160: D/CARLOZ(27942): <title>CARLOZ</title> 08-25 12:56:24.160: D/CARLOZ(27942): <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> 08-25 12:56:24.160: D/CARLOZ(27942): <link href="/static/favicon.ico" rel="shortcut icon" type="image/x-icon" /> 08-25 12:56:24.160: D/CARLOZ(27942): <link href="/static/favicon.ico" rel="icon" type="image/x-icon" /> 08-25 12:56:24.160: D/CARLOZ(27942): <link href="/static/default/css/base.css" rel="stylesheet" type="text/css" /> 08-25 12:56:24.160: D/CARLOZ(27942): <link href="/static/default/css/index.css" rel="stylesheet" type="text/css" /> 08-25 12:56:24.160: D/CARLOZ(27942): <link href="/static/default/css/moudle_g_search_bar.css" rel="stylesheet" type="text/css" /> 08-25 12:56:24.160: D/CARLOZ(27942): <link href="/static/default/css/moudle_g_taglist.css" rel="stylesheet" type="text/css" /> 08-25 12:56:24.160: D/CARLOZ(27942): <script src="/static/default/js/jquery-2.1.3.min.js" type="text/javascript"></script> 08-25 12:56:24.160: D/CARLOZ(27942): </head> 08-25 12:56:24.160: D/CARLOZ(27942): <body> 08-25 12:56:24.160: D/CARLOZ(27942): <div class="wrap"> 08-25 12:56:24.160: D/CARLOZ(27942): <div class="main" class="clearfix"> 08-25 12:56:24.160: D/CARLOZ(27942): <div class="header"> 08-25 12:56:24.160: D/CARLOZ(27942): <div class="top-bar"> 08-25 12:56:24.160: D/CARLOZ(27942): <div class="top-bar"> 08-25 12:56:24.160: D/CARLOZ(27942): <span class="index-page"><a href="/">CARLOZ</a> | </span> 08-25 12:56:24.170: D/CARLOZ(27942): 08-25 12:56:24.170: D/CARLOZ(27942): <span class="login-btn"><a href="/auth/login">Log in</a> | </span> 08-25 12:56:24.170: D/CARLOZ(27942): <span class="register-btn"><a href="/auth/regist">Register</a></span> 08-25 12:56:24.170: D/CARLOZ(27942): 08-25 12:56:24.170: D/CARLOZ(27942): </div> 08-25 12:56:24.170: D/CARLOZ(27942): </div> 08-25 12:56:24.170: D/CARLOZ(27942): <div class="g_search_bar"> 08-25 12:56:24.170: D/CARLOZ(27942): <a href="/"> 08-25 12:56:24.170: D/CARLOZ(27942): <span class="logo"> 08-25 12:56:24.170: D/CARLOZ(27942): <span class="logo_name">CARLO</span><span class="logo_rear">Z</span> 08-25 12:56:24.170: D/CARLOZ(27942): </span> 08-25 12:56:24.170: D/CARLOZ(27942): </a> 08-25 12:56:24.170: D/CARLOZ(27942): <form class="search_form" action="/search/"> 08-25 12:56:24.170: D/CARLOZ(27942): <input class="search_text" name="keywords" type="text" value=""/> 08-25 12:56:24.170: D/CARLOZ(27942): <input class="btn_submit" type="submit" value="search" /> 08-25 12:56:24.170: D/CARLOZ(27942): </form> 08-25 12:56:24.170: D/CARLOZ(27942): </div> 08-25 12:56:24.170: D/CARLOZ(27942): <div class="clear"></div> 08-25 12:56:24.170: D/CARLOZ(27942): </div> 08-25 12:56:24.170: D/CARLOZ(27942): <div class="g_taglist"> 08-25 12:56:24.170: D/CARLOZ(27942): <div id="personal_tag" class="tag-list hidden"> 08-25 12:56:24.170: D/CARLOZ(27942): </div> 08-25 12:56:24.170: D/CARLOZ(27942): <div id="public_tag" class="tag-list"> 08-25 12:56:24.170: D/CARLOZ(27942): <dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dt> <a href="/tag/?tag_name=Android">Android</a> </dt> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=ActivityManagerService">ActivityManagerService</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=WindowManagerService">WindowManagerService</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=PackageManagerService">PackageManagerService</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=SystemServer">SystemServer</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=PopupWindow">PopupWindow</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=PhoneWindow">PhoneWindow</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=ActivityThread">ActivityThread</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Handler">Handler</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=mk">mk</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): </dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dt> <a href="/tag/?tag_name=Python">Python</a> </dt> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Flask">Flask</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Web.py">Web.py</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Django">Django</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): </dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dt> <a href="/tag/?tag_name=ios">IOS</a> </dt> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=object-c">Object-c</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): </dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dt> <a href="/tag/?tag_name=J2EE">J2EE</a> </dt> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Struts">Struts</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Spring">Spring</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Hibernate">Hibernate</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): </dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dt> Web </dt> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=CSS">CSS</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=jQuery">jQuery</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=YUI">YUI</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Bootstrap">Bootstrap</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="/tag/?tag_name=Xml">Xml</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): </dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dl> 08-25 12:56:24.170: D/CARLOZ(27942): <dt> Cloud </dt> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="http://bce.baidu.com/">BCE</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="http://sae.sina.com.cn/">SAE</a> </dd> 08-25 12:56:24.170: D/CARLOZ(27942): <dd> . <a href="#">GAE</a> </dd> 08-25 12:56:24.190: D/CARLOZ(27942): <dd> . <a href="http://www.qiniu.com">Qiniu</a> </dd> 08-25 12:56:24.190: D/CARLOZ(27942): <dd> . <a href="http://www.aliyun.com/">Aliyun</a> </dd> 08-25 12:56:24.190: D/CARLOZ(27942): <dd> . <a href="http://www.qcloud.com/">QCloud</a> </dd> 08-25 12:56:24.190: D/CARLOZ(27942): </dl> 08-25 12:56:24.190: D/CARLOZ(27942): <dl> 08-25 12:56:24.190: D/CARLOZ(27942): <dt> Domain </dt> 08-25 12:56:24.190: D/CARLOZ(27942): <dd> . <a href="http://www.xinnet.com/">XinNet</a> </dd> 08-25 12:56:24.190: D/CARLOZ(27942): <dd> . <a href="https://www.dnspod.cn/">DNSPod</a> </dd> 08-25 12:56:24.190: D/CARLOZ(27942): </dl> 08-25 12:56:24.190: D/CARLOZ(27942): </div> 08-25 12:56:24.190: D/CARLOZ(27942): </div> 08-25 12:56:24.190: D/CARLOZ(27942): </div> 08-25 12:56:24.190: D/CARLOZ(27942): </div> 08-25 12:56:24.190: D/CARLOZ(27942): <div class="footer"> 08-25 12:56:24.190: D/CARLOZ(27942): <div class="copyright"> 08-25 12:56:24.190: D/CARLOZ(27942): Copyright ? 08-25 12:56:24.190: D/CARLOZ(27942): <strong><a href="/" target="_blank">CZLIB</a></strong> 08-25 12:56:24.190: D/CARLOZ(27942): All Rights Reversed. 08-25 12:56:24.190: D/CARLOZ(27942): 08-25 12:56:24.190: D/CARLOZ(27942): </div> 08-25 12:56:24.190: D/CARLOZ(27942): </div> 08-25 12:56:24.190: D/CARLOZ(27942): </body> 08-25 12:56:24.190: D/CARLOZ(27942): </html>
2. Http POST访问网站
Post方式需要向网络传输一部分数据,同时具有输入流和输出流
3. HttpClient进行Get方式通信
通过HttpClient建立网络链接,使用HttpGet方法读取数据,并且通过Response获取Entity返回值。
<EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnGetData" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/httpclient_get" /> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tvResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </ScrollView>
public void readNet(String url) { new AsyncTask<String, Void, String>() { @Override protected String doInBackground(String... params) { String urlString = params[0]; HttpGet get = new HttpGet(urlString); try { HttpResponse response = client.execute(get); String value = EntityUtils.toString(response.getEntity()); Log.d("CARLOZ", value); return value; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } protected void onPostExecute(String result) { tv.setText(result); }; }.execute(url); }
readNet("http://carloz.duapp.com/search/?keywords=" + et.getText());
效果如下:
4. HttpClient进行Post方式通信
通过HttpClient建立网络链接,使用HttpPost方法传出数据与读取数据,传出和传入的数据都是Entity的子类。
布局与3一样
public void readNet(String url, String keyword) { new AsyncTask<String, Void, String>() { @Override protected String doInBackground(String... params) { String urlString = params[0]; HttpPost post = new HttpPost(urlString); // StringEntity entity; List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); list.add(new BasicNameValuePair("keywords", params[1])); try { // BasicNameValuePair 用于提交表单数据 post.setEntity(new UrlEncodedFormEntity(list)); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } try { HttpResponse response = client.execute(post); String value = EntityUtils.toString(response.getEntity()); Log.d("CARLOZ", value); return value; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } protected void onPostExecute(String result) { tv.setText(result); }; }.execute(url, keyword); }
readNet("http://carloz.duapp.com/search", et.getText().toString());