android 使用post方式进行网络连接
2015-06-02 21:19
726 查看
private String connect(){ String uri = ETwebAddress.getText().toString(); StringBuilder sb =new StringBuilder(); BufferedReader in = null; if("".equals(uri)){ Toast.makeText(this,"empaty address",Toast.LENGTH_LONG).show(); }else{ HttpPost postRequest = new HttpPost(uri); ArrayList<NameValuePair> params = new ArrayList<>();//create name value pair arraylist params.add(new BasicNameValuePair("name","tom")); //add parameters to list params.add(new BasicNameValuePair("password","12345")); try{ postRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//set entity content in the post request HttpResponse response = new DefaultHttpClient().execute(postRequest);//get connection response if(response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){ in = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));//get stream String NL = System.getProperty("line.separator"); String line=""; sb = new StringBuilder(); while((line = in.readLine())!=null){ sb.append(line+NL); } in.close(); }else{ sb.append("connection failure..."); } }catch (Exception e){ e.printStackTrace(); Toast.makeText(this,"connection exception",Toast.LENGTH_LONG).show(); } } return sb.toString(); }
相关文章推荐
- 网络编程
- Android之Http通信——2.详解Http的消息头与响应头
- Socket 网络编程
- 网络通信框架Volley的学习(一)
- 网络请求的步骤:
- Android之Http通信——1.初识Http协议
- Java Http GET POST发送请求
- HTTP 1.1状态代码及其含义
- rtp协议详解/rtcp协议详解
- Namespace:Openstack的网络实现
- Ubuntu 14.1 配置NFS
- Linux常用网络命令(一)
- 经典http协议详解
- http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
- 网络状态监测之 Reachability的使用
- android网络相册(带磁盘缓存DiskLruCache 和内存缓存LruCache)
- Java编写网络爬虫笔记(第三部分:Jsoup的强大)
- HttpClient取得自定义的状态码302,并获取Header中的参数Location
- 正确处理浏览器在下载文件时HTTP头的编码问题(Content-Disposition)
- java 从网络Url中下载文件