2017-09-15 1 views
0

Ich habe einige Schwierigkeiten beim Versuch, eine Verbindung zur Datenbank in meiner Anwendung einzurichten.Android Studio Mysql PHP - Verbindung mit der Datenbank nicht möglich

Der PHP-Code funktioniert gut. Ich denke, das Problem ist mein Code im Android Studio. Es gibt keinen Fehler, aber wenn ich das Projekt mit dem Android-Emulator ausführe, kann ich mich nicht anmelden, weil es ein "falsches Passwort" anzeigt. Wenn ich versuche, mich anzumelden, wird "Konto erstellt" angezeigt, aber die Daten befinden sich nicht in der Datenbank. Also ich denke, es gibt ein Problem mit der Verbindung zur Datenbank. Meine Apps stellen keine Verbindung zur Datenbank her. Ich habe so viele Dinge ausprobiert, meine SQL-Abfragen doppelt überprüft, und sehe einfach nicht, warum es nicht funktioniert ...

Dies ist meine Login-Klasse.

package com.example.user.myapplication; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.google.gson.Gson; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 

import cz.msebera.android.httpclient.HttpEntity; 
import cz.msebera.android.httpclient.HttpResponse; 
import cz.msebera.android.httpclient.NameValuePair; 
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity; 
import cz.msebera.android.httpclient.client.methods.HttpPost; 
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient; 
import cz.msebera.android.httpclient.message.BasicNameValuePair; 
import cz.msebera.android.httpclient.params.BasicHttpParams; 

import java.net.HttpURLConnection; 
import java.net.URL; 

public class LoginActivity extends Activity { 

    String responseBody = "yas"; 

    Button signUpButton; 
    Button SigninBtn; 

    EditText UserIdEd; 
    EditText PasswordEd; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login2); 

     UserIdEd = (EditText)findViewById(R.id.IdLoginET); 
     PasswordEd = (EditText)findViewById(R.id.PasswordLoginET); 

     SigninBtn = (Button) findViewById(R.id.loginBtn); 

     SigninBtn.setOnClickListener(new View.OnClickListener() { 
             @Override 
             public void onClick(View v) { 

              SignInStudentToApp Temp = new SignInStudentToApp(); 
              Temp.execute(); 
             } 
            } 
     ); 

     signUpButton = (Button)findViewById(R.id.signUpBtn); 
     signUpButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(LoginActivity.this , SignUp.class)); 
      } 
     }); 
    } 


    public class SignInStudentToApp extends AsyncTask<String , Void , Boolean>{ 

     private ProgressDialog progress; 

     public SignInStudentToApp(){ 

      progress = new ProgressDialog(LoginActivity.this); 
     } 


     protected void onPreExecute() { 
      this.progress.setMessage("Wait please ..."); 
      this.progress.show(); 
     } 

     @Override 
     protected void onPostExecute(final Boolean success) { 
      if (progress.isShowing()) { 

       if(StudentInfo.student_info != null) { 
        Toast.makeText(LoginActivity.this, "Sign in Successfully", Toast.LENGTH_LONG).show(); 
        startActivity(new Intent(LoginActivity.this, WelcomeScreen.class)); 

       } 
       else 
        Toast.makeText(LoginActivity.this, "Wrong Password or Id", Toast.LENGTH_LONG).show(); 

      } 
      progress.dismiss(); 
     } 

     @Override 
     protected Boolean doInBackground(String... params) { 

      try { 
       return SignInCheck(); 
      } catch (UnsupportedEncodingException e) { 
       Log.d("ERORRR", e.getMessage()); 
      } 
      finally { 
       return false; 
      } 
     } 
    } 


    boolean SignInCheck() throws UnsupportedEncodingException { 

     Log.i("Yasser", "start sssssss"); 
     SignIn userSign = new SignIn(); 
     userSign.setId(UserIdEd.getText().toString()); 
     userSign.setPassword(PasswordEd.getText().toString()); 

     /* Building Parameters */ 

     DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
     HttpPost httppost = new HttpPost("http://10.0.2.2/ForSever.php"); 

     // Depends on your web service 
     //httppost.setHeader("Content-type", "application/json"); 

     InputStream inputStream = null; 
     String result = null; 
     try { 
      List<NameValuePair> parameters = new ArrayList<NameValuePair>(); 
      parameters.add(new BasicNameValuePair("StudentId",userSign.getId())); 
      parameters.add(new BasicNameValuePair("pass", userSign.getPassword())); 
      httppost.setEntity(new UrlEncodedFormEntity(parameters)); 
      Log.i("Tests", "start Task for Request"); 
      HttpResponse response = httpclient.execute(httppost); 
      Log.i("Teste", "End Task for Requsest"); 
      HttpEntity entity = response.getEntity(); 

      inputStream = entity.getContent(); 
      // json is UTF-8 by default 
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      while ((line = reader.readLine()) != null) 
      { 
       sb.append(line + "\n"); 
      } 
      result = sb.toString(); 
      Log.i("Test", result); 
     } catch (Exception e) { 
      // Oops 
     } 
     finally { 
      try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
     } 

     // responseBody = result ; 

     Gson gson = new Gson(); 
     // StudentInfo.student_info.Init(); 
     StudentInfo.student_info = gson.fromJson(result,StudentInfo.class); 

     Log.i("Test is vote", StudentInfo.student_info.isVote()+""); 

     return true; 
    } 


    @Override 
    protected void onPause() { 
     super.onPause(); 

     UserIdEd.setText("") ; 
     PasswordEd.setText(""); 
    } 
} 

Ich denke, vielleicht gibt es ein Problem mit diesem Abschnitt:

boolean SignInCheck() throws UnsupportedEncodingException { 

    Log.i("Yasser", "start sssssss"); 
    SignIn userSign = new SignIn(); 
    userSign.setId(UserIdEd.getText().toString()); 
    userSign.setPassword(PasswordEd.getText().toString()); 

    /* Building Parameters */ 

    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
    HttpPost httppost = new HttpPost("http://10.0.2.2/ForSever.php"); 

    // Depends on your web service 
    //httppost.setHeader("Content-type", "application/json"); 

    InputStream inputStream = null; 
    String result = null; 
    try { 
     List<NameValuePair> parameters = new ArrayList<NameValuePair>(); 
     parameters.add(new BasicNameValuePair("StudentId",userSign.getId())); 
     parameters.add(new BasicNameValuePair("pass", userSign.getPassword())); 
     httppost.setEntity(new UrlEncodedFormEntity(parameters)); 
     Log.i("Tests", "start Task for Request"); 
     HttpResponse response = httpclient.execute(httppost); 
     Log.i("Teste", "End Task for Requsest"); 
     HttpEntity entity = response.getEntity(); 

     inputStream = entity.getContent(); 
     // json is UTF-8 by default 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
     result = sb.toString(); 
     Log.i("Test", result); 
    } catch (Exception e) { 
     // Oops 
    } 
    finally { 
     try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
    } 

    // responseBody = result ; 

    Gson gson = new Gson(); 
    // StudentInfo.student_info.Init(); 
    StudentInfo.student_info = gson.fromJson(result,StudentInfo.class); 

    Log.i("Test is vote", StudentInfo.student_info.isVote()+""); 

    return true; 
} 


@Override 
protected void onPause() { 
    super.onPause(); 

    UserIdEd.setText("") ; 
    PasswordEd.setText(""); 
} 
+0

Sie nähern sich zu einem MySQL-Server verbinden ist ziemlich sonderbar. Sie sollten einen alternativen Ansatz über API versuchen. –

Antwort

Verwandte Themen