2016-08-24 7 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

はまた、私は私のサーバー用に入力したポート番号があったが、間違っている。私もそれを修正しました。そして今はすべてがうまくいくようです。皆さん、助けてくれてありがとう!

関連する問題