http通信,以xml或者json为载体,相互通信数据。

Android对于http的网络通信,提供了标准的java接口——httpURLConnection接口,以及apache的接口——httpclient接口。其中我自己用的比较多的而是httpclient这个接口,因为它的功能更为丰富很有效。

同时http通信也分为post方式和get的方式,两个相比较的话,post传送的数据量比较大,安全性也比较高。因此在做数据查询时,我会用get方式;而在做数据添加、修改或删除时,我就用Post方式
以下是基于httpclient接口,用get和post的方式通信的代码。
(get方式)

protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.login);       TextView text = (TextView) this.findViewById(R.id.text_http);       String httpurl = “http://10.0.2.2:8080/SIM_SERVER/org/Org-list”;       //①httpget连接对象       HttpGet httpRequest = new HttpGet(httpurl);       //②取得httpclient的对象       HttpClient httpclient = new DefaultHttpClient();       try {         //③请求httpclient,取得httpResponse           HttpResponse httpResponse = httpclient.execute(httpRequest);    //④判断请求是否成功     if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){             //⑤取得返回的字符串              String strResult = EntityUtils.toString(httpResponse.getEntity());              text.setText(strResult);           }else{              text.setText(“请求失败”);           }       } catch (ClientProtocolException e) {           e.printStackTrace();       } catch (IOException e) {           e.printStackTrace();       }    }(post方式)protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       this.setContentView(R.layout.login);       TextView text = (TextView) this.findViewById(R.id.text_http);       //①http连接地址       String url = “http://10.0.2.2:8080/SIM_SERVER/and/Android-test”;        //②取得httppost连接对象       HttpPost httpPost = new HttpPost(url);        //③使用NameValuePair来保存要传递的Post参数       List params = new ArrayList();        //④添加要传递的参数       params.add(new BasicNameValuePair(“parentId”, “0″));        HttpEntity httpentity;       try {            //⑤设置字符集           httpentity = new UrlEncodedFormEntity(params, “utf-8″);            //⑥把字符集设置在请求request里面           httpPost.setEntity(httpentity);             //⑦取得httpclient           HttpClient httpClient = new DefaultHttpClient();             //⑧请求发送,并获得response           HttpResponse httpResponse = httpClient.execute(httpPost);//⑨判断请求是否成功     if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){              //⑩取得返回字符串              String strResult = EntityUtils.toString(httpResponse.getEntity());              text.setText(strResult);           }else{              text.setText(“请求失败”);           }       } catch (UnsupportedEncodingException e) {           e.printStackTrace();       } catch (ClientProtocolException e) {           e.printStackTrace();       } catch (IOException e) {           e.printStackTrace();       }     }


更多相关文章

  1. [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的
  2. Intent filter 关于Action、Category属性详解---附带实例源码
  3. Android(安卓)RxJava 实际应用讲解:网络请求嵌套回调
  4. Android实现加载富文本以及SpannableString、SpannableStringBui
  5. Android(安卓)内功心法(1.5)——android常用设计模式之命令模式
  6. 网络编程之——他山之石OkHttp
  7. android 定制 View派生类
  8. 『转』Android(安卓)推送方式
  9. Android(安卓)Async HTTP Clients: Volley vs Retrofit

随机推荐

  1. Google Play服务API(位置服务)是否需要数据
  2. android自定义通知,android自定义Notifica
  3. Android_单元测试_依赖注入_入门
  4. 使用drawables的IntelliJ Android缓存构
  5. Android超级common的Adapter,也就是传说的
  6. Android(Java)简单发送和接收服务器 - 快速
  7. 谷歌横幅广告未能加载广告:0
  8. 9PNG用的很不舒服,请高手指导!
  9. Android-----------扫描二维码
  10. Android 开发之 Android 开发的起步