• 采用get方式提交数据到服务器实例


    GetDemo项目目录

    一、编写StreamTools.java

    /**
     * 
     */
    package com.hyzhou.getdemo.utiils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    
    /**
     * @author hyzhou
     *
     * 2013-11-6
     */
    public class StreamTools {
    
        /**
         * 把输入流内容转化成字符串
         */
        public static String readInputStream(InputStream is) {        
            try {
                ByteArrayOutputStream baos=new ByteArrayOutputStream();
                int len=0;
                byte[] buffer=new byte[1024];
                while ((len=is.read(buffer))!=-1) {
                    baos.write(buffer,0,len);
                }
                is.close();
                baos.close();
                byte[] result=baos.toByteArray();
                //试着解析result中的字符串
                String temp=new String(result);
                return temp;
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                return "获取失败";
            }
            
        }
    }
    View Code

    二、编写LoginServer.java

    /**
     * 
     */
    package com.hyzhou.getdemo.service;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import com.hyzhou.getdemo.utiils.StreamTools;
    
    /**
     * @author hyzhou
     *
     * 2013-11-6
     */
    public class LoginServer {
        public static String loginByGet(String username,String password)
        {
            
            try {
                String path="http://192.168.1.54:8080/web/LoginServlet?username="+username+"&password="+password;
                URL url=new URL(path);
                HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("GET");
                int code=conn.getResponseCode();
                if (code==200) {
                    InputStream is=conn.getInputStream();
                    String text=StreamTools.readInputStream(is);
                    return text;
                }else {
                    return null;
                }
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }
    View Code

    三、编写MainActivity.java

    package com.hyzhou.getdemo;
    
    import com.hyzhou.getdemo.service.LoginServer;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private EditText et_username, et_password;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et_username = (EditText) findViewById(R.id.et_username);
            et_password = (EditText) findViewById(R.id.et_password);
        }
    
        public void click(View view) {
            final String username = et_username.getText().toString().trim();
            final String password = et_password.getText().toString().trim();
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    final String result = LoginServer
                            .loginByGet(username, password);
                    if (result != null) {
                        runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this, result, 0).show();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                Toast.makeText(MainActivity.this, "请求失败", 0).show();
                            }
                        });
                    }
                }
            }).start();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    View Code

    注意:因为要请求网络,所以要在AndroidManifest.xml注册请求网络权限<uses-permission android:name="android.permission.INTERNET"/>

  • 相关阅读:
    分布式系统之CAP原理
    分布式缓存一致性哈希算法
    数据库三范式 无重复列 完全依赖主键 属性不依赖非主属性
    二叉树 B-树B+树
    数据库索引 主键 聚集索引 非聚集索引
    数据库水平拆分
    线程池ScheduledThreadPoolExecutor
    线程池之ThreadPoolExecutor
    mybatis一级缓存和二级缓存
    vue框架的搭建
  • 原文地址:https://www.cnblogs.com/hyzhou/p/3410749.html
Copyright © 2020-2023  润新知