我在尝试在我的应用程序中建立与数据库的连接时遇到了一些困难。
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("");
}
答案 0 :(得分:0)
您好,这是一个功能齐全的示例项目,它将使用php脚本从Android应用程序操作mysql数据库。我用排球来发出http请求。
https://github.com/chandima-b-samarasinghe/android-mysql-php
希望这会对你有所帮助:)。