Android Post请求SQL数据库

做了个登陆和注册,写下里,后面做参考用!

网上方法很多,由于菜鸟级别,一直没是成功,后来经多方努力,总结如下:

服务端方法: 

  一  、参数获取

        //GET请求参数获取
        //method = Request.QueryString["method"];
        //name = Request.QueryString["str_Name"];
        //psw = Request.QueryString["str_Psw"];
        //email = Request.QueryString["Email"];

        //POST请求参数获取
        method = Request.Form["method"];
        name = Request.Form["Name"];
        psw = Request.Form["Password"];
        email = Request.Form["Email"];

二、方法

   注册:

 public void RegistMsg(String UserName, String Password, String Emailbox)
    {
        String conn = "Server=.;DataBase=XCRain;user=sa;password=123";
        SqlConnection connection = new SqlConnection(conn);
        SqlCommand command = new SqlCommand("INSERT INTO registMsg VALUES ('" + UserName + "','" + Password + "','" +Emailbox + "')", connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
        Response.Write("{\"returnResult\":{\"desc\":\"注册成功\",\"result_code\":2}}");//输出JSON格式
    }

登陆:

 public void  LoginMsg(String UserName, String UserPassword, String Emailbox)
    {
        String conn = "Server=10.19.1.48;DataBase=XCRain;user=sa;password=123";
        SqlConnection connection = new SqlConnection(conn);
        SqlCommand command = new SqlCommand("SELECT * FROM registMsg WHERE Name='"+UserName+"'"+"AND Password='"+UserPassword+"'"+"AND Email='"+Emailbox+"'", connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
        object  count = command.ExecuteScalar();     //这个着实费了时间,判断数据库中有无记录的
        if(count==null)
        {
            Response.Write("{\"returnResult\":{\"desc\":\"没有此用户\",\"result_code\":0}}");
        }
        if(count!=null)
        {
            Response.Write("{\"returnResult\":{\"desc\":\"登陆成功\",\"result_code\":1}}");
        }

    }

 

三、客户端请求

              EditText loginName=(EditText)findViewById(R.id.inputName);
              EditText loginPsw=(EditText)findViewById(R.id.inputPsw);
              EditText loginEmail=(EditText)findViewById(R.id.inputEmail);
              String str_Name=loginName.getText().toString().trim();
              String str_Psw=loginPsw.getText().toString().trim();
              String Email=loginEmail.getText().toString().trim();   //trim()最好加上,我就因为每加而多了空格导致不成功,一直没发现,日!

       //POST请求
           try
           {
            String url=service_url;
            HttpPost request=new HttpPost(url);
            List<NameValuePair>params=new ArrayList <NameValuePair>();
            params.add(new BasicNameValuePair("method",method));
            params.add(new BasicNameValuePair("Name",str_Name));
            params.add(new BasicNameValuePair("Password",str_Psw));
            params.add(new BasicNameValuePair("Email",Email));
            request.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
            HttpResponse response=new DefaultHttpClient().execute(request);
            if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode())
               {
                       String strResult = EntityUtils.toString(response.getEntity()); 
                      JSONObject jsonObj = new JSONObject(strResult).getJSONObject("returnResult");  
                      String   Desc = jsonObj.getString("desc");

             }

            catch(Exception e)
           {
            e.printStackTrace();
           }     
    一般都采用POST请求,get请求参数都在url中,不安全

   下面是GET请求

//           String url=service_url+"?method="+method+"&str_Name="+str_Name+"&str_Psw="+str_Psw+"&Email="+Email;
//           try
//           {
//             HttpGet request = new HttpGet(url);
//             HttpClient httpClient = new DefaultHttpClient();
//                HttpResponse response = httpClient.execute(request);
//               if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode())
//                  {
//                 String strResult = EntityUtils.toString(response.getEntity()); 
//                 JSONObject jsonObj = new JSONObject(strResult).getJSONObject("returnRsult");  
//                 String   Desc = jsonObj.getString("desc");
//              }
//           catch(Exception e)
//               {
//              e.printStackTrace();
//            }
      

 url参数可以动态添加
//              StringBuilder sb=new StringBuilder();
//              sb.append(service_url);
//              sb.append("?method="+method);
//              sb.append("&str_Name"+Name);
//              sb.append("&str_Psw"+Password);
//              String str=sb.toString();  //要转化成字符串!!!!!!一定要注意!!!

总结:

      导致一直不成功的原因很多,第一大原因是不认真,很多时候是由于自己马虎代码写错,并且一直找不吃原因,最头疼那些不报错却不成功的时候了

      获取EditText文本时,没加trim(),也是好久才发现

      源码关联失败,网上的方法也试了,不行,最后重新换了个android.jar包,我觉得不是包的原因,应该还是代码错误

      开始我是注册POST请求,登陆GET请求,丫的也不成功,不知道为啥,难道不能同时发送两个请求?我觉得可以,以后再试试。

你可能感兴趣的:(Android Post请求SQL数据库)