gpt4 book ai didi

java - 我想制作搜索框以通过android studio从mysql获取数据

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

有人可以帮助我吗?我可以通过 android studio 将数据插入 mysql。但我不知道如何从 mysql 获取数据到 android studio。请帮助我并通过我的代码显示示例或答案以供更多理解。(抱歉我的英语不好。)(这不是错误,但我想要行会线。:( )

Conn.php  
<?php
$db_name = "employee101";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);
?>
>
>

登录.php

require "conn.php";
$name = $_POST["name"];
$surname = $_POST["surname"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$mysql_qry = "insert into employee_data(name, surname, age, username, password) value ('$name','$surname','$age','$username','$password')";
if($conn->query($mysql_qry) === TRUE){
echo "Insert Success";
}
else
{
echo"Insert not success".$mysql_qry."<br>". $conn->error;
}
$conn->close();

背景woker.java

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;

/**
* Created by puen on 4/6/2016.
*/
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.10.125/login.php";
String register_url = "http://192.168.10.125/register.php";
if(type.equals("login")){
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while ((line = bufferedReader.readLine())!= null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}else if (type.equals("register")){
try {
String name = params[1];
String surname = params[2];
String age = params[3];
String username = params[4];
String password = params[5];
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
+URLEncoder.encode("surname","UTF-8")+"="+URLEncoder.encode(surname,"UTF-8")+"&"
+URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(age,"UTF-8")+"&"
+URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while ((line = bufferedReader.readLine())!= null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
return null;
}

@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");

}

@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}

注册.java

package com.example.puen.mysqldemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Register extends AppCompatActivity {
EditText name, surname, age, username, password;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
name = (EditText)findViewById(R.id.et_name);
surname = (EditText)findViewById(R.id.et_surname);
age = (EditText)findViewById(R.id.et_age);
username = (EditText)findViewById(R.id.et_username);
password = (EditText)findViewById(R.id.et_password);



}
public void OnReg(View view){
String str_name = name.getText().toString();
String str_surname = surname.getText().toString();
String str_age = age.getText().toString();
String str_username = username.getText().toString();
String str_password = password.getText().toString();

String type = "register";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, str_name, str_surname, str_age, str_username, str_password);
}

}

最佳答案

如果要从 mysql 检索用户的详细信息,请使用 JSON 对信息进行编码。代码看起来有点像这样:

<?php
$username = $_POST['username'];

require_once('Connect.php');

$sql = "select * from account_info where username='$username';";

$res = mysql_query($sql);
$num=mysql_num_rows($res);
if($num>0)
{
$result= array();
while($row=mysql_fetch_array($res))
array_push($result, array('name'=>$row[1],'phone'=>$row[2]));

echo json_encode(array("result"=>$result));

}
else
{
echo "No accounts ";
}
mysql_close($conn);
?>

然后使用 JSONObject 在 Android 中的 JSONArray 中检索此信息。

例如:

让 JSONString 成为通过异步任务将查询发送到 php 脚本后得到的结果。

JSONObject jsonObject = new JSONObject(JSONString); 数组 = jsonObject.getJSONArray(JSON_ARRAY);

关于java - 我想制作搜索框以通过android studio从mysql获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36517937/

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