gpt4 book ai didi

php - Android Studio Mysql PHP - 无法连接数据库

转载 作者:行者123 更新时间:2023-11-29 18:25:15 25 4
gpt4 key购买 nike

我在尝试在应用程序中建立与数据库的连接时遇到一些困难。

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 数据库。我使用volley来发出http请求。

https://github.com/chandima-b-samarasinghe/android-mysql-php

希望这对你有帮助:)

关于php - Android Studio Mysql PHP - 无法连接数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46247621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com