开发android程序我们会遇到各种关于网络请求
所以我们开发的过程中,难免会用关于网络操作的
java.net.*,又有org.apache.http.*,
在数据处理方面支持json,xml等常用的数据格式。
JSON 的优势是数据轻便,小巧,
XML 比较笨重,但是它的存在自然有他的道理
apche.httpclient
- public void get() {
- String url = httpUrl + "?text1=" + text1.getText().toString()
- + "&text2=" + text2.getText().toString();
- // 创建HttpGet对象
- HttpGet request = new HttpGet(url);
- // 创建HttpClient对象
- HttpClient client = new DefaultHttpClient();
- HttpResponse httpResponse = null;
- try {
- httpResponse = client.execute(request);
- if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- text3.setText(EntityUtils.toString(httpResponse.getEntity(),
- "utf-8"));
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void get() {
- String url = httpUrl + "?text1=" + text1.getText().toString()
- + "&text2=" + text2.getText().toString();
- // 创建HttpGet对象
- HttpGet request = new HttpGet(url);
- // 创建HttpClient对象
- HttpClient client = new DefaultHttpClient();
- HttpResponse httpResponse = null;
- try {
- httpResponse = client.execute(request);
- if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- text3.setText(EntityUtils.toString(httpResponse.getEntity(),
- "utf-8"));
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
B:POST方式操作
- public void post() {
- // 创建HttpPost对象
- HttpPost httpRequest = new HttpPost(httpUrl);
- // 创建传递参数集合
- List params = new ArrayList();
- params.add(new BasicNameValuePair("text1", text1.getText().toString()));
- params.add(new BasicNameValuePair("text2", text2.getText().toString()));
- // 设置字符集
- try {
- HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
- httpRequest.setEntity(entity);
- // 创建连接对象
- HttpClient client = new DefaultHttpClient();
- // 执行连接
- HttpResponse response = client.execute(httpRequest);
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- text3.setText(EntityUtils.toString(response.getEntity(),
- "utf-8"));
- }
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void post() {
- // 创建HttpPost对象
- HttpPost httpRequest = new HttpPost(httpUrl);
- // 创建传递参数集合
- List params = new ArrayList();
- params.add(new BasicNameValuePair("text1", text1.getText().toString()));
- params.add(new BasicNameValuePair("text2", text2.getText().toString()));
- // 设置字符集
- try {
- HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
- httpRequest.setEntity(entity);
- // 创建连接对象
- HttpClient client = new DefaultHttpClient();
- // 执行连接
- HttpResponse response = client.execute(httpRequest);
- if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- text3.setText(EntityUtils.toString(response.getEntity(),
- "utf-8"));
- }
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }