zoukankan      html  css  js  c++  java
  • 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();
        }
    
  • 相关阅读:
    输入输出那些事
    NYOJ 20
    NYOJ 283
    HDU 1285
    HDU 2639(第K大背包)
    HDU 4288
    对Visual Studio C++ hash_map严谨一点的测试转载
    vc6,vc.net,vc7,vc8,vc9,c,c++,c#的区别与联系
    我在南大的七年刘末鹏
    慎用Visual Studio C++默认的hash_map转载
  • 原文地址:https://www.cnblogs.com/jackwuyongxing/p/4547699.html
Copyright © 2011-2022 走看看