P
Peter200980
Ambitioniertes Mitglied
- 0
Hallo Zusammen,
ich versuche mich gerade daran, einen Login für meine App zu basteln.
Immer wenn ich versuche von der app aus einen Eintrag in die SQL Datenbank hinzuzufügen (per neuregistrierung) kommt eine Meldung:"Unable to Save data to the Database"
Anbei schicke ich euch noch meine PHP dateien die MainActivity und die Register Klasse
MainActivity:
. Vielleicht hat jemand eine Idee.
init.php
Register.php
Login.php:
Schöne Grüße
Peter
ich versuche mich gerade daran, einen Login für meine App zu basteln.
Immer wenn ich versuche von der app aus einen Eintrag in die SQL Datenbank hinzuzufügen (per neuregistrierung) kommt eine Meldung:"Unable to Save data to the Database"
Anbei schicke ich euch noch meine PHP dateien die MainActivity und die Register Klasse
MainActivity:
Code:
package de.androidnewcomer.login_forpp;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.widget.EditText;import org.json.JSONException;import org.json.JSONObject;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import static android.R.attr.name;import static android.R.attr.password;/** * Created by Hartl on 03.01.2017. */public class MainActivity extends Activity { EditText name, password; String Name, Password; String NAME=null, PASSWORD=null, EMAIL=null; Context ctx=this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = (EditText) findViewById(R.id.main_name); password = (EditText) findViewById(R.id.main_password); } public void main_register(View v){ startActivity(new Intent(this,Register.class)); } public void main_login(View v){ Name = name.getText().toString(); String Password = password.getText().toString(); BackGround b = new BackGround(); b.execute(Name, Password); } class BackGround extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { String name = params[0]; String password = params[1]; String data=""; int tmp; try { URL url = new URL("http://www.partypeople.bplaced.net/login.php"); String urlParams = "name="+name+"&password="+password; HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); OutputStream os = httpURLConnection.getOutputStream(); os.write(urlParams.getBytes()); os.flush(); os.close(); InputStream is = httpURLConnection.getInputStream(); while((tmp=is.read())!=-1){ data+= (char)tmp; } is.close(); httpURLConnection.disconnect(); return data; } catch (MalformedURLException e) { e.printStackTrace(); return "Exception: "+e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "Exception: "+e.getMessage(); } } @Override protected void onPostExecute(String s) { String err=null; try { JSONObject root = new JSONObject(s); JSONObject user_data = root.getJSONObject("user_data"); NAME = user_data.getString("name"); PASSWORD = user_data.getString("password"); EMAIL = user_data.getString("email"); } catch (JSONException e) { e.printStackTrace(); err = "Exception: "+e.getMessage(); } Intent i = new Intent(ctx, MainActivity.class); i.putExtra("name", NAME); i.putExtra("password", PASSWORD); i.putExtra("email", EMAIL); i.putExtra("err", err); startActivity(i); } } }
init.php
Code:
<?php
error_reporting(0);
$db_name = "geheim";
$mysql_user = "geheim";
$mysql_pass = "geheim";
$server_name = "localhost";
$con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);
if(!$con){
echo '{"message":"Unable to connect to the database."}';
}
?>
Register.java
Code:
package de.androidnewcomer.login_forpp;import android.app.Activity;import android.content.Context;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;/** * Created by Hartl on 03.01.2017. */public class Register extends Activity { EditText name, password, email; String Name, Password, Email; Context ctx=this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register); name = (EditText) findViewById(R.id.register_name); password = (EditText) findViewById(R.id.register_password); email = (EditText) findViewById(R.id.register_email); } public void register_register(View v){ Name = name.getText().toString(); Password = password.getText().toString(); Email = email.getText().toString(); BackGround b = new BackGround(); b.execute(Name, Password, Email); } class BackGround extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { String name = params[0]; String password = params[1]; String email = params[2]; String data=""; int tmp; try { URL url = new URL("http://www.partypeople.bplaced.net/register.php"); String urlParams = "name="+name+"&password="+password+"&email="+email; HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); OutputStream os = httpURLConnection.getOutputStream(); os.write(urlParams.getBytes()); os.flush(); os.close(); InputStream is = httpURLConnection.getInputStream(); while((tmp=is.read())!=-1){ data+= (char)tmp; } is.close(); httpURLConnection.disconnect(); return data; } catch (MalformedURLException e) { e.printStackTrace(); return "Exception: "+e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "Exception: "+e.getMessage(); } } @Override protected void onPostExecute(String s) { if(s.equals("")){ s="Data saved successfully."; } Toast.makeText(ctx, s, Toast.LENGTH_LONG).show(); } }}
Register.php
Code:
<?php
error_reporting(0);
require "init.php";
$name = $_POST["name"];
$password = $_POST["password"];
$email = $_POST["email"];
//$name = "sdf";
//$password = "sdf";
//$email = "sdf@r54";
$sql = "INSERT INTO `user_info` (`id`,`name`, `password`, `email`) VALUES (NULL, '".$name."', '".$password."', '".$email."');";
if(!mysqli_query($con, $sql)){
echo '{"message":"Unable to save the data to the database."}';
}
?>
Login.php:
Code:
<?php
error_reporting(0);
require "init.php";
$name = $_POST["name"];
$password = $_POST["password"];
//$name = "sdf";
//$password = "sdf";
$sql = "SELECT * FROM `user_info` WHERE `name`='".$name."' AND `password`='".$password."';";
$result = mysqli_query($con, $sql);
$response = array();
while($row = mysqli_fetch_array($result)){
$response = array("id"=>$row[0],"name"=>$row[1],"password"=>$row[2],"email"=>$row[3]);
}
echo json_encode(array("user_data"=>$response));
?>
Peter