Android 网络编程
2014-04-28 15:04
441 查看
,在JAVA中进行网络通信,我们需要定义一个HttpPost对象,此对象需要输入url参数。然后定义一个List<NameValuePair>对象来以JSON的形式存储数据。接着通过HttpPost的getParams().setParameter()方法进行一些必要参数的设置,例如连接超时的时间与接收数据超时的时间。然后通过HttpResponse httpResponse = new DefaultHttpClient().execute(request); 语句来发送Http请求并获取服务器端传来的数据。此时的数据为String类型,需要吧它转换成JSONObject,以便后面的分析数据与获取我们需要的数据。
网络通讯关键代码:
网络通讯关键代码:
HttpPost request = new HttpPost(url); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username","I am username" ); params.add(new BasicNameValuePair("password", "I am password"); params.add(new BasicNameValuePair("verName", verName)); request.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); request.getParams().setParameter( CoreConnectionPNames.SO_TIMEOUT, 10000); request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse httpResponse = new DefaultHttpClient() .execute(request); String retSrc = EntityUtils.toString(httpResponse .getEntity()); JSONObject result = new JSONObject(retSrc);
相关文章推荐
- quick-cocos2dx CCHTTPRequest 使用
- POJ 1251(http://poj.org/problem?id=1251)
- 【翻译】企业级SOA之路——在Web Service中使用HTTP和JMS
- 帧中继下的OSPF的网络类型
- 遥感影像的人工神经网络分类
- HTTP Method小结
- 解决MySQL建立连接问题,快速回收复用TCP的TIME_WAIT
- iOS开发ASIHTTPRequest下载数据
- iOS开发ASIHttpRequest创建和执行request
- iOS开发ASIHTTPRequest直接读取磁盘数据流请求体
- iOS开发ASIHTTPRequest身份验证
- iOS开发ASIHTTPRequest进度追踪
- Android4.0 之后不允许在主线程进行网络连接
- 网络收集的系列学习贴,大家有好的内容也可跟帖回复
- iOS开发ASIHTTPRequest数据压缩
- iOS开发ASIHTTPRequest中Cookie的使用
- iOS开发ASIHTTPRequest断点续传(下载)
- iOS开发ASIHTTPRequest使用download cache
- java请求WEB服务器 获取页面返回,且支持HTTPS,获取cret证书
- Android HttpGet和HttpPost设置超时