首页 新闻 会员 周边 捐助

Android端和mySQL端如何进行交互

0
悬赏园豆:100 [已关闭问题] 关闭于 2014-08-13 19:34

最近在做一个关于图书馆的小应用,在做登录验证时,希望通过mysql数据库进行验证,请问如何使Android端的数据发送到服务器端,并完成登录验证,希望得到大师指点,谢谢。

小破孩123的主页 小破孩123 | 初学一级 | 园豆:109
提问于:2014-08-13 15:24
< >
分享
所有回答(4)
-2

部署mySql的PC写个服务程序,然后安卓端与这个服务进行通信。

Firen | 园豆:5385 (大侠五级) | 2014-08-13 15:42
1

服务端提供一个api接口,返回一个验证成功与否的信息呗。

幻天芒 | 园豆:37207 (高人七级) | 2014-08-13 15:47

能否给一个相关的源代码,刚接触这点,不懂。谢谢

支持(0) 反对(0) 小破孩123 | 园豆:109 (初学一级) | 2014-08-13 15:54

@小破孩123: 简单实现就是WebService,Android端访问这个WebService,传递用户名密码,返回true false,判断是否成功登录...

支持(0) 反对(0) 幻天芒 | 园豆:37207 (高人七级) | 2014-08-13 16:29
-1

WebService

[秦时明月] | 园豆:738 (小虾三级) | 2014-08-13 16:46

具体讲一下行吗?

 

支持(0) 反对(0) 小破孩123 | 园豆:109 (初学一级) | 2014-08-13 19:26
1

经过自己的摸索,大概已经解决了,首先在服务器端设置一个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;
    }

}
小破孩123 | 园豆:109 (初学一级) | 2014-08-13 19:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册