让我给您一些有关我的数据库的信息。我使用xampp连接到MySQL。
我的问题:
以下证明我的编码没有错误。
以下证明我的应用无法检测数据库。
我认为我对BufferedReader
使用了错误的ISO编码。 Youtube告诉我,不同的android studio具有不同的ISO编码。
我的数据库连接确实是真的吗?
这是我的一些代码。
数据库连接:background.java
package com.example.whatisthat;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class background extends AsyncTask <String, Void, String> {
AlertDialog dialog;
Context context;
public background(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute() {
dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String s) {
dialog.setMessage(s);
dialog.show();
}
@Override
protected String doInBackground(String... voids) {
String result = "";
String user = voids[0];
String pass = voids[1];
String connstr = "http://localhost:8080/login1.php";
try
{
URL url = new URL (connstr);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
OutputStream ops = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops, "UTF-8"));
String data = URLEncoder.encode("user", "UTF-8")+"="+URLEncoder.encode(user, "UTF-8")
+"&&"+URLEncoder.encode("pass", "UTF-8")+"="+URLEncoder.encode(pass, "UTF-8");
writer.write(data);
writer.flush();
writer.close();
ops.close();
InputStream ips = http.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ips, "ISO-8859-1"));
String line = "";
while ((line = reader.readLine()) != null)
{
result += line;
}
reader.close();
ips.close();
http.disconnect();
return result;
}
catch (MalformedURLException e)
{
result = e.getMessage();
}
catch (IOException e)
{
result = e.getMessage();
}
return result;
}
}
我的登录页面:login1.java
package com.example.whatisthat;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.sql.Connection;
public class Login1 extends AppCompatActivity {
Button btnLogin;
EditText etUsername, etPass;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login1);
btnLogin = findViewById(R.id.Login);
etUsername = findViewById(R.id.Username);
etPass = findViewById(R.id.Password);
}
public void btnRegister(View view) {
startActivity(new Intent(getApplicationContext(), SignUp.class));
}
public void loginBtn(View view) {
String user = etUsername.getText().toString();
String pass = etPass.getText().toString();
background bg = new background(this);
bg.execute(user, pass);
}
}
我的登录页面XML文件:activity_login1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Login1"
android:gravity="center">
<ImageView
android:layout_width="345dp"
android:layout_height="200dp"
android:padding="30dp"
android:src="@drawable/people" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="345dp"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColorHint="@color/colorPrimary">
<EditText
android:id="@+id/Username"
android:layout_width="306dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="Username "
android:inputType="text"
android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="344dp"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColorHint="@color/colorPrimary"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/Password"
android:layout_width="307dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/Login"
android:layout_width="281dp"
android:layout_height="60dp"
android:layout_marginTop="40dp"
android:background="@drawable/button_rounded"
android:text="Login "
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:onClick="loginBtn" />
<Button
android:id="@+id/Register"
android:layout_width="281dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:background="@drawable/button_rounded"
android:onClick="btnRegister"
android:text="Register "
android:textColor="@color/colorAccent"
android:textSize="20sp" />
</LinearLayout>
我的讲师说我为连接数据库输入了错误的IP地址。有人可以帮助我使此代码成功连接到我的数据库。