android 使用post方式进行网络连接
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(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。