D
Deepkey
Neues Mitglied
- 0
Hey liebe Community ,
Ich bin seit drei Monaten in einer Informatiker (Fian) Ausbildung und programmiere neben bei an meinen Projekt und
hab da ein kleines Problem. Vlt. steh ich auch einfach nur auf den Schlauch oder mein Ansatz ist komplett falsch.
Ich hab eine Login abfrage von username und passwort, wenn diese erfolgreich ist soll dann erst die userarea geladen werden.
Verbindung zur Datenbank steht und php Script gibt JSon Antwort.
bei der if (loginsend) funktioniert es nur mit (!).
wäre cool wenn ihr mir helfen könnt.
hier erstmal der Code.
die Main ↓
Die LoginRequest ↓
Ich bin seit drei Monaten in einer Informatiker (Fian) Ausbildung und programmiere neben bei an meinen Projekt und
hab da ein kleines Problem. Vlt. steh ich auch einfach nur auf den Schlauch oder mein Ansatz ist komplett falsch.
Ich hab eine Login abfrage von username und passwort, wenn diese erfolgreich ist soll dann erst die userarea geladen werden.
Verbindung zur Datenbank steht und php Script gibt JSon Antwort.
bei der if (loginsend) funktioniert es nur mit (!).
wäre cool wenn ihr mir helfen könnt.
hier erstmal der Code.
die Main ↓
Code:
package com.example.root.framespielerei;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.root.framespielerei.Loginrequest.success;
public class MainActivity extends Activity{
Button login;
TextView registerNow;
EditText et_username, et_passwort;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerNow = findViewById(R.id.tv_register_now);
login = findViewById(R.id.btn_login);
}
public void login(View view) {
MainActivity.super.onPause();
setContentView(R.layout.loginform);
Button back,login;
back = findViewById(R.id.btn_back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.super.onResume();
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this,"You go back",Toast.LENGTH_LONG).show();
}
});
login = findViewById(R.id.btn_loginsend);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginsend();
if (!loginsend()){
Toast.makeText(MainActivity.this,"jippi",Toast.LENGTH_LONG).show();
}
}
});
}
boolean loginsend() {
et_username = findViewById(R.id.et_username);
et_passwort = findViewById(R.id.et_passwort);
String username = et_username.getText().toString();
String passwort = et_passwort.getText().toString();
new Loginrequest(this).execute(username,passwort);
Toast.makeText(this, "Checkt LogIn .....\n in arbeit", Toast.LENGTH_SHORT).show();
return success;
}
/* public void loginsend(View view) {
et_username = findViewById(R.id.et_username);
et_passwort = findViewById(R.id.et_passwort);
String username = et_username.getText().toString();
String passwort = et_passwort.getText().toString();
new Loginrequest(this).execute(username,passwort);
Toast.makeText(this, "Checkt LogIn .....\n in arbeit", Toast.LENGTH_SHORT).show();
Loginrequest.gibtrue(success);
if (!success){
Toast.makeText(MainActivity.this,"jippi",Toast.LENGTH_LONG).show();
}
}*/
}
Die LoginRequest ↓
Code:
package com.example.root.framespielerei;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
class Loginrequest extends AsyncTask<String,Void,String>{
@SuppressLint("StaticFieldLeak")
Context context;
public static boolean success;
Loginrequest(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... dit) {
String username = dit[0];
String passwort = dit[1];
String link;
String data;
BufferedReader bf;
String result;
try {
data = "?username=" + URLEncoder.encode(username, "UTF-8");
data += "&password=" + URLEncoder.encode(passwort, "UTF-8");
link = "http://10.0.2.2/UserLogin.php" + data;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bf = new BufferedReader(new InputStreamReader(con.getInputStream()));
result = bf.readLine();
return result;
}catch (Exception e){
return ("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
if (result != null) {
try {
JSONObject jsonObj = new JSONObject(result);
String query_result = jsonObj.getString("query_result");
switch (query_result) {
case "SUCCESS":
success = true;
Toast.makeText(context, "Erfolgreich eingelogt..\nDaten werden geladen.", Toast.LENGTH_SHORT).show();
break;
case "FAILURE":
success = false;
Toast.makeText(context, "Dieser User existiert nicht \n oder Passwort falsch!", Toast.LENGTH_SHORT).show();
success = false;
break;
case "FAILURETOP":
success = false;
Toast.makeText(context, "nüchscht rein nüscht keine verbindung.", Toast.LENGTH_LONG).show();
break;
default:
success = false;
Toast.makeText(context, "Server nicht Erreichbar...\n checken sie die verbindung!", Toast.LENGTH_LONG).show();
break;
case "USEREMPTYERR":
success = false;
Toast.makeText(context, "Es wurde kein Username eingegeben\n bitte erneut versuchen", Toast.LENGTH_LONG).show();
break;
case "USERPASSEMPTYERR":
success = false;
Toast.makeText(context, "Es wurde keine LoginDaten eingegeben\n bitte erneut versuchen", Toast.LENGTH_LONG).show();
break;
case "PASSEMPTYERR":
success = false;
Toast.makeText(context, "Es wurde kein Passwort eingegeben\n bitte erneut versuchen", Toast.LENGTH_LONG).show();
break;
case "USERNOTEXIST":
success = false;
Toast.makeText(context, "Sry User gibbet nich\n bitte erneut versuchen", Toast.LENGTH_LONG).show();
break;
}
}catch (JSONException e){
e.printStackTrace();
success = false;
Toast.makeText(context, "fehler in der Verbindungsdatei\nerrorRequest (0 011ef8 ).", Toast.LENGTH_LONG).show();
}
}
}
}