部署mySql的PC写个服务程序,然后安卓端与这个服务进行通信。
服务端提供一个api接口,返回一个验证成功与否的信息呗。
能否给一个相关的源代码,刚接触这点,不懂。谢谢
@小破孩123: 简单实现就是WebService,Android端访问这个WebService,传递用户名密码,返回true false,判断是否成功登录...
WebService
具体讲一下行吗?
经过自己的摸索,大概已经解决了,首先在服务器端设置一个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; } }