2016-08-24 48 views
0

我有一个活动,用户输入他/她的名字,用户名和密码。我想将这些信息作为MySQL数据库中的条目发送。这是我的代码:Android - 发送登录信息到MySQL数据库

protected String doInBackground(String... params) { 

    String reg_url = "http://MyIPAddress:ServerPort#/Register.php"; 



     String name = params[1]; 
     String user_name = params[2]; 
     String user_pass = params[3]; 


     try { 


      URL url = new URL(reg_url); 

      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 

      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoOutput(true); 

      OutputStream OS = httpURLConnection.getOutputStream(); 

      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 


      String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" + 
        URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + 
        URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8"); 


      bufferedWriter.write(data); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      OS.close(); 


      return "Registration Success!"; 


     } catch (MalformedURLException e) { 

      e.printStackTrace(); 

     } catch (IOException e) { 

      e.printStackTrace(); 
     } 


    return null; 

} 

该代码运行时没有任何错误,但数据库在程序执行后仍为空。这是我的PHP文件(Register.php):

<?php 

    require "Connection.php"; //Establishes connection to database 


    $name = $_POST["user"]; 
    $user_name = $_POST["user_name"]; 
    $user_pass = $_POST["user_pass"]; 

    $sql_query = "insert into user_info values('$name','$user_name','$user_pass');"; 

    if (mysqli_real_query($link, $sql_query)) { 

     echo "<h1> Data Insertion Success! </h1>"; 

    } 

    else { 

    echo "Data Insertion Error..."; 


    } 


?> 

并连接到数据库(Connection.php)代码:

<?php 


    $link = mysqli_init(); 

    $success = mysqli_real_connect($link,$host,$user,$password,$db,$port); 


    if($success) { 

     echo "<h1> Success! </h1>"; 

    } 


    else { 

     echo "Error...".mysqli_connect_error(); 

    } 

?> 

我在这里失去了一些东西?我很乐意感谢你的帮助。谢谢!

+0

发布此数据时运行的服务器端代码在哪里 – RiggsFolly

+0

我们不通过YouTube教程来PLOW来追上你做错了什么。请阅读如何创建[最小,完整和可验证示例](http://stackoverflow.com/help/mcve) – RiggsFolly

+0

您是否检查过serer收到请求?如果是的话,你是否在这方面进行了调试,以查看其失败的地方? –

回答

0

下面的代码我最终使用到的数据发送到doInBackground方法中我的数据库:

try{ 


      String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" + 
        URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + 
        URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8"); 

      URL url = new URL(reg_url); 
      URLConnection conn = url.openConnection(); 
      conn.setDoOutput(true); 

      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
      wr.write(data); 
      wr.flush(); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      StringBuilder sb = new StringBuilder(); 
      String line; 
      // Read Server Response 
      while((line = reader.readLine()) != null) 
      { 
       sb.append(line); 
       break; 
      } 

      Log.e("TAG", sb.toString()); 

      return sb.toString(); 


     }catch(Exception e){ 
      return new String("Exception: " + e.getMessage()); 
     } 

参考:Android login to mysql database

也变成了我为我的服务器输入的端口号为不正确。我修正了这一点,现在一切似乎都很好。感谢你们所有人的帮助!