• 采用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"/>

  • 相关阅读:
    哈夫曼(Huffman)编码
    面向对象的3个基本要素和5个基本设计原则(整理)
    面向对象设计原则OO
    Java多线程中start()和run()的区别
    HBase入门
    SparkGraphXTest.scala
    IntellijIdea中常用的快捷键
    SparkSQLTest.scala
    SparkStreamingTest.scala
    (转)理解POST和PUT的区别,顺便提下RESTful
  • 原文地址:https://www.cnblogs.com/hyzhou/p/3410749.html
Copyright © 2020-2023  润新知