安卓和服务器交换数据

来源:互联网 发布:sql查找两列重复的 编辑:程序博客网 时间:2024/05/13 21:27

先在服务器端设置一个select来接收Android端发送过来的数据,具体的数据验证属于jsp内容了,在此不多说。具体代码:

web端:ManageServlet.java

package cn.acm.edu.hpu.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ManageServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        response.setCharacterEncoding("utf-8");        String name = request.getParameter("name");        String password = request.getParameter("password");        System.out.println("用户名:"+name+"  密码:"+password);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {    }}

Android端:MainActivity.java

public class MainActivity extends Activity {        private  EditText textname = null;    private  EditText textpassword = null;    private  Button button = null;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                textname = (EditText)findViewById(R.id.name);        textpassword = (EditText)findViewById(R.id.password);        button = (Button)findViewById(R.id.button);                button.setOnClickListener(new mybuttonlistener());            }        class mybuttonlistener implements OnClickListener{                boolean result=false;                public void onClick(View v) {                        String name = textname.getText().toString();            String password = textpassword.getText().toString();                                    try {                result = NewsService.save(name,password);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                        if(result){                Toast.makeText(MainActivity.this, R.string.ok, Toast.LENGTH_SHORT).show();            }else{                Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();            }                    }            }    }
import javax.net.ssl.HttpsURLConnection;public class NewsService {    /**     * 登录验证     * @param name  姓名     * @param password  密码     * @return     */        public static boolean save(String name, String password){                String path = "http://10.20.90.3:8080/Register/ManageServlet";                Map<String, String> student = new HashMap<String, String>();        student.put("name", name);        student.put("password", password);                try {            return SendGETRequest(path, student);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                return false;            }        /**     * 发送GET请求     * @param path   请求路径     * @param student   请求参数     * @return  请求是否成功     * @throws Exception     */    private static boolean SendGETRequest(String path, Map<String, String> student) throws Exception{                // http://10.20.90.3:8080/Register/ManageServlet?name=1233&password=abc                StringBuilder url = new StringBuilder(path);        url.append("?");        for(Map.Entry<String, String> map : student.entrySet()){            url.append(map.getKey()).append("=");            url.append(map.getValue());            url.append("&");        }                url.deleteCharAt(url.length()-1);        System.out.println(url);        HttpsURLConnection conn = (HttpsURLConnection)new URL(url.toString()).openConnection();        conn.setConnectTimeout(100000);        conn.setRequestMethod("GET");        if(conn.getResponseCode() == 200){            return true;        }        return false;    }}
0 0
原创粉丝点击