[OFFEN] mysql Abfrage über App?

H

haner

Ambitioniertes Mitglied
0
Hallo zusammen,
ich habe mich nun daran versucht meine zuvor im Webbrowser getestete mysql-Abfrage in eine Android-App zu integrieren. Nun stecke ich aber mal wieder in Schwierigkeiten und benötige eure Hilfe. Leider passiert nichts, wenn ich auf den Login-Button klicke. Habe mal ein paar Stellen im Code rot markiert. Ich vermute, dass der Fehler dort liegt. Vielleicht habt ihr es dann einfacher.

Danke haner

Hier sind die Codes:

conn.php
<?php
//Stellt Verbindung zur mysql-Datenbank der Tabelle Fleisch her
$db_name = "techcad";
$mysql_username = "techcad";
$mysql_password = "**********";
$server_name = "localhost";
$Fleisch = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
?>

login.php
<?php
require "conn.php";
$user_name = $_POST["Benutzername"];
$user_pass = $_POST["Passwort"];

$mysql_qry = "SELECT * FROM Fleisch Where Benutzername LIKE '$user_name' AND Passwort LIKE '$user_pass';";
$result = mysqli_query($Fleisch, $mysql_qry);
if(mysqli_num_rows($result) > 0) {
echo "Login erfolgreich";
}
else {
echo "Login nicht erfolgreich";
}
?>

mainActivity.java
package com.ta.mar.appangebote;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

// edittextvariable definieren für benutzername und Passwort
EditText BenutzernameEt, PasswortEt;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BenutzernameEt = (EditText)findViewById(R.id.etBenutzername);
PasswortEt = (EditText)findViewById(R.id.etPasswort);
}

public void OnLogin(View view) {
String Benutzername = BenutzernameEt.getText().toString();
String Passwort = PasswortEt.getText().toString();
String type = "login";

//Instanz der BackgroundWorkerclass definieren
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, Benutzername, Passwort);
}
}

BackgroundWorker.java
package com.ta.mar.appangebote;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

public class BackgroundWorker extends AsyncTask<String,Void,String> {

//contaxtvariable definieren
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx) {
context = ctx;
}

@override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://www.techcad.bplaced.net/login.php";
if(type.equals("login")) {
try {
String Benutzername = params[1];
String Passwort = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("Benutzername", "UTF-8")+"="+URLEncoder.encode(Benutzername, "UTF-8")+"&"
+URLEncoder.encode("Passwort", "UTF-8")+"="+URLEncoder.encode(Passwort, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while((line = bufferedReader.readLine()) != null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

@override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("LoginStatus");
}

@override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}

@override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

public void execute(String type, String Benutzername, String Passwort) {
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.ta.mar.appangebote.MainActivity">

<EditText
android:id="@+id/etBenutzername"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/etPasswort"
android:layout_width="wrap_content"
android:layout_height="46dp"
android:layout_marginStart="16dp"
android:layout_marginTop="2dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etBenutzername" />

<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Login"
app:layout_constraintEnd_toEndOf="@+id/etPasswort"
app:layout_constraintTop_toBottomOf="@+id/etPasswort"
android:onClick="OnLogin"/>

</android.support.constraint.ConstraintLayout>
phpmyadmin.JPG
 
Hallo,

was sagt den der LogCat?

Gruß
 
In der Logcat steht das hier:
12-18 10:00:55.723 3997-4034/com.ta.mar.appangebote W/EGL_emulation: eglSurfaceAttrib not implemented
12-18 10:00:55.723 3997-4034/com.ta.mar.appangebote W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa49ae040, error=EGL_SUCCESS

Kann damit leider nichts anfangen
VG haner
 
Da ich auf dem ersten Blick auch keinen Fehler entdecke: Hast du schon die Internet-Permission gesetzt? Müsste eigentlich in deinem Logcat angezeigt werden.
 
Ja, habe ich gesetzt --> siehe hier:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ta.mar.appangebote">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@Style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Habe es nun mit dem Emulator und auf meinem Smartphone getestet. Jeweils ohne Erfolg. Es passiert einfach gar nichts, wenn man den Button anklickt.
VG haner
 
Dann musst du wohl "von Hand" debuggen.
Erstell dir einen Logeintrag in deiner "OnLogin" Methode und such nach diesem String in deinem Logcat, ob er überhaupt diesen Text da reinschreibt, danach sehen wir weiter.

Edit: Bitte formatier deinen Code hier mit "\[CODE\]" Tags.
 
Puh da bin ich jetzt leider überfragt. Kannst du mir das ein bischen genauer erklären wie ich das erstellen soll?
Beschäftige mich noch nicht so lange mit java und Android.
Bin dir aber schonmal sehr dankbar für deine bisherige Hilfe.
 

Ähnliche Themen

B
Antworten
3
Aufrufe
1.286
swa00
swa00
5
Antworten
22
Aufrufe
1.368
590239
5
R
  • Robby1950
2
Antworten
23
Aufrufe
954
Robby1950
R
Zurück
Oben Unten