[英]Android Studio Mysql PHP - unable to connect with database
我在嘗試在應用程序中建立與數據庫的連接時遇到一些困難。
PHP代碼可以正常工作。 我認為問題是我在Android Studio中的代碼。 沒有錯誤,但是當我使用Android模擬器運行項目時,無法登錄,因為它顯示“密碼錯誤”。 當我嘗試注冊時,它顯示“已創建帳戶”,但數據不在數據庫中。 因此,我認為與數據庫的連接存在問題。 我的應用程序未連接到數據庫。 我已經嘗試了很多事情,仔細檢查了我的SQL查詢,只是不明白為什么它不起作用...
這是我的登錄課程。
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("");
}
}
我認為這部分可能有問題:
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("");
}
您好,這里是一個功能齊全的示例項目,該項目將使用php腳本從android應用中操作mysql databsase。 我用凌空抽煙發出http請求。
https://github.com/chandima-b-samarasinghe/android-mysql-php
希望這個能對您有所幫助 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.