网络数据的处理之HttpClient
来源:互联网 发布:2017ipad视频软件 编辑:程序博客网 时间:2024/03/29 07:53
这一章我们主要来介绍网络数据的传递与处理,相信很多读者都希望做出来的应用能跟网络上的数据进行互动,如微博,论坛之类的,这里我们就要学习网络传输与返回数据的处理,首先网络传递参数有POST跟GET两种协议,做过网页或是学习过的同学应该知道.网页每个表单中都有一个<form action="XXX" method="post">参数,这里method就是提交表单参数使用的协议,当然,协议不止这两种,还有文件上传协议,这我们以后会讲,今天我们首来就来熟悉Android中对于POST跟GET协议的应用,首先我们提供了一个HttpConnectionUtil.java的辅助类,这里面对POST跟GET进行了封装
import java.io.BufferedReader;
- import
java.io.IOException; - import
java.io.InputStreamReader; - import
java.io.UnsupportedEncodingExcep tion; - import
java.net.URLEncoder; - import
java.util.ArrayList; - import
java.util.List; - import
java.util.Map; -
- import
org.apache.http.HttpResponse; - import
org.apache.http.HttpStatus; - import
org.apache.http.NameValuePair; - import
org.apache.http.client.ClientProtocolException; - import
org.apache.http.client.HttpClient; - import
org.apache.http.client.entity.UrlEncodedFormEntity; - import
org.apache.http.client.methods.HttpGet; - import
org.apache.http.client.methods.HttpPost; - import
org.apache.http.client.methods.HttpUriRequest; - import
org.apache.http.impl.client.DefaultHttpClient; - import
org.apache.http.message.BasicNameValuePair; -
- import
android.os.Handler; - import
android.util.Log; -
- public
class HttpConnectionUtil - {
-
static enum HttpMethod -
{ -
GET, POST -
} -
-
-
void asyncConnect( finalString finalurl, HttpMethod method, -
HttpConnectionCallback callback) -
{ -
asyncConnect(url, method, callback); -
} -
-
-
void syncConnect( finalString finalurl, HttpMethod method, -
HttpConnectionCallback callback) -
{ -
syncConnect(url, method, callback); -
} -
-
-
void asyncConnect( finalString url, -
Map<String, finalString> params, HttpMethod method, -
HttpConnectionCallback callback) -
{ -
Handler handler = Handler(); -
Runnable runnable = Runnable() -
{ -
void run() -
{ -
syncConnect(url, params, method, callback); -
} -
}; -
handler.post(runnable); -
} -
-
-
void syncConnect( finalString finalurl, Map<String, String> params, -
HttpMethod finalmethod, HttpConnectionCallback callback) -
{ -
String json = -
BufferedReader reader = -
-
-
{ -
HttpClient client = DefaultHttpClient(); -
HttpUriRequest request = getRequest(url, params, method); -
HttpResponse response = client.execute(request); -
(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) -
{ -
reader = BufferedReader( newInputStreamReader(response -
.getEntity().getContent())); -
StringBuilder sb = StringBuilder(); -
(String null;s = reader.readLine(); s != s = reader -
.readLine()) -
{ -
sb.append(s); -
} -
-
json = sb.toString(); -
} -
} (ClientProtocolException e) -
{ -
Log.e( e.getMessage(), e); -
} (IOException e) -
{ -
Log.e( e.getMessage(), e); -
} -
{ -
-
{ -
(reader null)!= -
{ -
reader.close(); -
} -
} (IOException e) -
{ -
ignore me -
} -
} -
callback.execute(json); -
} -
-
-
HttpUriRequest getRequest(String url, Map<String, String> params, -
HttpMethod method) -
{ -
(method.equals(HttpMethod.POST)) -
{ -
List<NameValuePair> listParams = ArrayList<NameValuePair>(); -
(params null)!= -
{ -
(String name : params.keySet()) -
{ -
listParams.add( BasicNameValuePair(name, params -
.get(name))); -
} -
} -
-
{ -
UrlEncodedFormEntity entity = UrlEncodedFormEntity( -
listParams); -
HttpPost request = HttpPost(url); -
request.setEntity(entity); -
request; -
} (UnsupportedEncodingExcep tion e) -
{ -
Should not come here, ignore me. -
new java.lang.RuntimeException(e.getMessage(), e); -
} -
} -
{ -
(url.indexOf( "?")< 0) -
{ -
url += -
} -
(params null)!= -
{ -
(String name : params.keySet()) -
{ -
-
{ -
url += + "="name + -
+ URLEncoder.encode(params.get(name), -
-
} (UnsupportedEncodingExcep tion e) -
{ -
e.printStackTrace(); -
} -
} -
} -
HttpGet request = HttpGet(url); -
request; -
} -
} -
-
-
interface HttpConnectionCallback -
{ -
-
execute(String response); -
} -
- }
这个类也是我从网上看到的,使用起来相当方便,希望读者能学会怎样使用,其实像java学习,可以将一些有用的类或是方法定义个自己包,将它们放进去,下次要用的话只要在主程序中调用就行了,这也是面向对象重要的方法.
这里面的方法,我就没有一行一行定义说明了,里面用的都是HttpClient的中方法
接下来,我们用这个类来进行Android的应用
main.xml(模板文件)
<?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" -
android:layout_width="match_parent" -
-
android:layout_width="match_parent" -
android:id="@+id/http_edit" -
-
-
-
android:layout_width="match_parent" -
android:id="@+id/relativeLayout1"> -
android:text="取消" android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_alignParentRight="true"></Button> -
android:text="确定" android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:layout_toLeftOf="@+id/http_cancal" -
-
-
android:layout_width="match_parent" -
-
android:id="@+id/http_text" android:text="TextView" -
-
android:layout_width="match_parent"></TextView> -
-
- </LinearLayout>
然后就是主Actitiv的java代码了
import android.app.Activity;
- import
android.os.Bundle; - import
android.text.Html; - import
android.view.View; - import
android.view.View.OnClickListener; - import
android.widget.Button; - import
android.widget.EditText; - import
android.widget.TextView; -
- import
com.kang.http.HttpConnectionUtil; - import
com.kang.http.HttpConnectionUtil.HttpConnectionCallback; - import
com.kang.http.HttpConnectionUtil.HttpMethod; -
- public
class HttpClientDemo extendsActivity - {
-
Button ok_btn; -
Button cancal_btn; -
EditText edit_text; -
TextView text; -
-
-
void onCreate(Bundle savedInstanceState) -
{ -
TODO Auto-generated method stub -
-
setContentView(R.layout.http_client); -
-
-
ok_btn = (Button) findViewById(R.id.http_ok); -
ok_btn.setOnClickListener( ClickEvent()); -
-
-
cancal_btn = (Button) findViewById(R.id.http_cancal); -
cancal_btn.setOnClickListener( ClickEvent()); -
-
-
edit_text = (EditText) findViewById(R.id.http_edit); -
-
-
text = (TextView) findViewById(R.id.http_text); -
} -
-
-
-
class ClickEvent implementsOnClickListener -
{ -
-
-
void onClick(View v) -
{ -
(v.getId()) -
{ -
R.id.http_ok: -
-
String url = edit_text.getText().toString().trim(); -
-
(!url.equals( "http://")&& ""))!url.equals( -
{ -
-
HttpConnectionUtil conn = HttpConnectionUtil(); -
conn.asyncConnect(url, HttpMethod.GET, -
HttpConnectionCallback() -
{ -
-
-
void execute(String response) -
{ -
text.setText(Html.fromHtml(response)); -
} -
}); -
} -
-
-
-
R.id.http_cancal: -
edit_text.setText( -
-
-
-
} -
-
} -
-
} - }
看里面
你一定会奇怪,怎么会有其他一些代码呢?呵呵,这里我们取出的是它的源代码.OK,这一章讲完了,下一章我们就要来实现Mysql数据库+PHP+Android的显示了,相信这会是很多读者感兴趣的一章,谢谢
- 网络数据的处理之HttpClient
- Android[中级教程]第九章 网络数据的处理之HttpClient
- Httpclient之网络请求数据
- HttpClient之网络请求数据
- android网络数据请求之HttpClient
- Android网络数据请求之HttpClient
- android网络处理HttpClient
- 使用httpClient请求的网络数据
- httpClient请求及返回数据的处理
- HttpClient获取网络数据
- httpclient网络请求数据
- HttpClient请求网络数据
- HttpClient网络请求数据
- HttpClient获取网络数据
- HttpClient 获取网络数据
- 网络请求之HttpClient 类的使用
- 网络爬虫之httpclient的使用
- android的网络通信之HttpClient
- ASP.NET MVC 笔记
- HTML5本地存储不完全指南
- C# 学习笔记
- NCMsg_Test
- Oracle数据库表空间日常操作
- 网络数据的处理之HttpClient
- 6.087 Practical Programming in C, Assign1(not complete)
- 设置Android程序图标
- 刷新指定目录下的模块信息
- 虚拟机安装mac os
- Mysql+Json+Android的组合
- 插入排序
- 逃逸分析(Escape Analysis)
- jquery ajax跨域访问完整例子