gpt4 book ai didi

java - 如何将 EditText 中的 PHP 变量的值传递到 Android Studio?

转载 作者:行者123 更新时间:2023-12-02 11:27:23 26 4
gpt4 key购买 nike

我想问你是否知道如何将 PHP 中的变量值传递到 android studio 中的 EditText 中?让我更好地解释一下,我有一个 Android 应用程序,其中有 2 个表单,在其中输入信息并将其保存在 MySQL 数据库中。数据库具有第一个表单的表和第二个表单的表,这两个表是链接的,然后第二个表具有与随第一个表单插入的第一个表的主键关联的外部键。

我不能做的是,一旦我在第一个表中输入数据并通过mysqli Insert_id()获取主键,我不知道如何传递变量的值在第二个表单中存在的编辑文本中的 $People_id 中。

下面留下我的android代码,第二种形式的代码有问题,因为输入的数据没有到达PHP脚本。

总结一下问题,有谁知道如何在php文件中传递使用mysql insert id获得的主键并将该值放入第二种形式中的EditText中?

第一个表单将数据保存到表“Person”中,该表中的字段是:

  • ID(PK)
  • 姓名 (VARCHAR)

第二个表单将数据保存在表“Person2”中,该表中的字段为:

  • idP2(PK)
  • FK_persone(外键)
  • Squadra、Ruolo 和 Numero_maglia (VARCHAR)
PS。在第二种形式中,数据保存在表“People2”中,字段“Ruolo”、“Squadra”和“Numero_maglia”由用户输入,但字段“id_People2”必须已插入(变量值必须是有 $ _SESSION ['People_id'] = $People_id; 我从第一种形式的 PHP 脚本中获得的值)。

第一个表单可以工作,idata保存在DataBase中,但是第二个表单不起作用,android studio .JAVA文件有问题,但我不明白是什么问题:(

第一种形式的java文件是:

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;


import android.app.ProgressDialog;

import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;

import android.text.TextUtils;
import android.widget.Toast;
import java.net.FileNameMap;
import java.util.HashMap;


public class Activity1 extends AppCompatActivity {

private EditText nome, cognome;

private Button registrazione, login;

//REGISTRAZIONE
String F_Name_Holder, L_Name_Holder;
String finalResult ;
String HttpURLRegister = "http://provaord.altervista.org/NEW/RRR.php";
Boolean CheckEditText ;
ProgressDialog progressDialog;
HashMap<String,String> hashMap = new HashMap<>();
HttpParse httpParse = new HttpParse();





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);




nome = (EditText)findViewById(R.id.editText6);
cognome = (EditText)findViewById(R.id.editText7);

registrazione = (Button)findViewById(R.id.button5);
login = (Button)findViewById(R.id.button3);



registrazione.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

// Checking whether EditText is Empty or Not
CheckEditTextIsEmptyOrNot();

if(CheckEditText){

// If EditText is not empty and CheckEditText = True then this block will execute.

UserRegisterFunction(F_Name_Holder,L_Name_Holder);

}
else {

// If EditText is empty then this block will execute .
Toast.makeText(Activity1.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();

}


}
});



}



//REGISTRAZIONE
public void CheckEditTextIsEmptyOrNot(){

F_Name_Holder = nome.getText().toString();
L_Name_Holder = cognome.getText().toString();



if(TextUtils.isEmpty(F_Name_Holder) || TextUtils.isEmpty(L_Name_Holder) )
{

CheckEditText = false;

}
else {

CheckEditText = true ;
}

}


//REGISTRATION
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
public void UserRegisterFunction(final String Nome, final String Cognome){

class UserRegisterFunctionClass extends AsyncTask<String,Void,String> {

@Override
protected void onPreExecute() {
super.onPreExecute();




progressDialog = ProgressDialog.show(Activity1.this,"Loading Data",null,true,true);
}

@Override
protected void onPostExecute(String httpResponseMsg) {

super.onPostExecute(httpResponseMsg);

progressDialog.dismiss();

Toast.makeText(Activity1.this,httpResponseMsg.toString(), Toast.LENGTH_LONG).show();



if(httpResponseMsg.equalsIgnoreCase("Registration Successfully")){

finish();

Intent intent = new Intent(Activity1.this, Activity2.class);



startActivity(intent);

}

}

//REGISTRATION
@Override
protected String doInBackground(String... params) {

hashMap.put("Nome",params[0]);

hashMap.put("Cognome",params[1]);



finalResult = httpParse.postRequest(hashMap, HttpURLRegister);

return finalResult;
}
}

UserRegisterFunctionClass userRegisterFunctionClass = new UserRegisterFunctionClass();

userRegisterFunctionClass.execute(Nome,Cognome);
}



}

第二种形式的java文件是:

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;


import android.app.ProgressDialog;

import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;

import android.text.TextUtils;
import android.widget.Toast;
import java.net.FileNameMap;
import java.util.HashMap;

public class Activity2 extends AppCompatActivity {

private EditText id, squadra, ruolo, numeromaglia;

private Button registrazionee;

//REGISTRAZIONE
String Squadra_Holder, Ruolo_Holder, Numero_Maglia_Holder;
String finalResult ;
String HttpURLRegister = "http://provaord.altervista.org/NEW/R22.php";
Boolean CheckEditText ;
ProgressDialog progressDialog;
HashMap<String,String> hashMap = new HashMap<>();
HttpParse httpParse = new HttpParse();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);

// id = (EditText)findViewById(R.id.editText6);
squadra = (EditText)findViewById(R.id.editText7);
ruolo = (EditText)findViewById(R.id.editText3);
numeromaglia = (EditText)findViewById(R.id.editText4);

registrazionee = (Button)findViewById(R.id.button55);

registrazionee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

// Checking whether EditText is Empty or Not
CheckEditTextIsEmptyOrNot();

if(CheckEditText){

// If EditText is not empty and CheckEditText = True then this block will execute.

UserRegisterFunction(Squadra_Holder, Ruolo_Holder, Numero_Maglia_Holder);

}
else {

// If EditText is empty then this block will execute .
Toast.makeText(Activity2.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();

}


}
});



}





//REGISTRAZIONE
public void CheckEditTextIsEmptyOrNot(){

Squadra_Holder = squadra.getText().toString();
Ruolo_Holder = ruolo.getText().toString();
Numero_Maglia_Holder = numeromaglia.getText().toString();



if(TextUtils.isEmpty(Squadra_Holder) || TextUtils.isEmpty(Ruolo_Holder) || TextUtils.isEmpty(Numero_Maglia_Holder) )
{

CheckEditText = false;

}
else {

CheckEditText = true ;
}

}





//REGISTRATION
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
public void UserRegisterFunction(final String Squadra, final String Ruolo, final String Numero_maglia){

class UserRegisterFunctionClass extends AsyncTask<String,Void,String> {

@Override
protected void onPreExecute() {
super.onPreExecute();




progressDialog = ProgressDialog.show(Activity2.this,"Loading Data",null,true,true);
}

@Override
protected void onPostExecute(String httpResponseMsg) {

super.onPostExecute(httpResponseMsg);

progressDialog.dismiss();

Toast.makeText(Activity2.this,httpResponseMsg.toString(), Toast.LENGTH_LONG).show();



if(httpResponseMsg.equalsIgnoreCase("Registration Successfully")){

finish();

Intent intent = new Intent(Activity2.this, Activity3.class);



startActivity(intent);

}

}

//REGISTRATION
@Override
protected String doInBackground(String... params) {

hashMap.put("Squadra",params[0]);

hashMap.put("Ruolo",params[1]);

hashMap.put("Numero_maglia",params[2]);





finalResult = httpParse.postRequest(hashMap, HttpURLRegister);

return finalResult;
}
}

UserRegisterFunctionClass userRegisterFunctionClass = new UserRegisterFunctionClass();

userRegisterFunctionClass.execute(Squadra, Ruolo, Numero_maglia);
}


}

HttpParse.java 文件的 java 代码是这样的:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
* Created by Juned on 3/3/2017.
*/

public class HttpParse {

String FinalHttpData = "";
String Result ;
BufferedWriter bufferedWriter ;
OutputStream outputStream ;
BufferedReader bufferedReader ;
StringBuilder stringBuilder = new StringBuilder();
URL url;

public String postRequest(HashMap<String, String> Data, String HttpUrlHolder) {

try {
url = new URL(HttpUrlHolder);

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

httpURLConnection.setReadTimeout(14000);

httpURLConnection.setConnectTimeout(14000);

httpURLConnection.setRequestMethod("POST");

httpURLConnection.setDoInput(true);

httpURLConnection.setDoOutput(true);

outputStream = httpURLConnection.getOutputStream();

bufferedWriter = new BufferedWriter(

new OutputStreamWriter(outputStream, "UTF-8"));

bufferedWriter.write(FinalDataParse(Data));

bufferedWriter.flush();

bufferedWriter.close();

outputStream.close();

if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {

bufferedReader = new BufferedReader(
new InputStreamReader(
httpURLConnection.getInputStream()
)
);
FinalHttpData = bufferedReader.readLine();
}
else {
FinalHttpData = "Something Went Wrong";
}
} catch (Exception e) {
e.printStackTrace();
}

return FinalHttpData;
}

public String FinalDataParse(HashMap<String,String> hashMap2) throws UnsupportedEncodingException {

for(Map.Entry<String,String> map_entry : hashMap2.entrySet()){

stringBuilder.append("&");

stringBuilder.append(URLEncoder.encode(map_entry.getKey(), "UTF-8"));

stringBuilder.append("=");

stringBuilder.append(URLEncoder.encode(map_entry.getValue(), "UTF-8"));

}

Result = stringBuilder.toString();

return Result ;
}
}

第一个表单的 PHP 文件:

<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){

include 'C.php';

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

$Nome = $_POST['Nome'];
$Cognome = $_POST['Cognome'];


$CheckSQL = "SELECT * FROM Persone WHERE Nome = '$Nome'";

$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));

if(isset($check)){

echo 'Utente già registrato';

}
else{
$Sql_Query = "INSERT INTO Persone (Nome, Cognome) values ('$Nome','$Cognome')";



if(mysqli_query($con,$Sql_Query))
{
echo 'Registration Successfully';
$People_id = mysqli_insert_id($con);
$_SESSION ['People_id'] = $People_id;

}
else
{
echo 'Something went wrong';
}
}

mysqli_close($con);
}

?>

第二种形式的 PHP 文件:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
print_r($_POST);
if($_SERVER['REQUEST_METHOD']=='POST'){

include 'C.php';

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

$Squadra = $_POST['Squadra'];
$Ruolo = $_POST['Ruolo'];
$Numero_maglia = $_POST['Numero_maglia'];


$CheckSQL = "SELECT * FROM Persone, Persone2 WHERE Persone2.FK_persone = '" . ($_SESSION ['People_id']). "'";;

$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));

if(isset($check)){

echo 'Utente già registrato';

}
else{
$Sql_Query = "INSERT INTO Persone2 (FK_persone, Squadra, Ruolo, Numero_maglia) values ('" . ($_SESSION ['People_id']). "','$Squadra', '$Ruolo', '$Numero_maglia')";



if(mysqli_query($con,$Sql_Query))
{
echo 'Registration Successfully';
}
else
{
echo 'Something went wrong';
}
}
mysqli_close($con);
}

?>

文件 XML 为第二种形式:

<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.bruzi.myord.Activity2">

<EditText
android:id="@+id/editText6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="ID_People2"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Squadra"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText6" />

<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Ruolo"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText7" />

<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Numero maglia"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText3" />

<Button
android:id="@+id/button55"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="32dp"
android:layout_marginEnd="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:background="@android:color/holo_blue_light"
android:text="Registrazione"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText4" />
</android.support.constraint.ConstraintLayout>

最佳答案

这是一个漫长的旅程,步骤如下:

首先:您需要确保 php 服务器以 xml 或 json 格式返回所需的值,如果需要,您可以通过创建 Web 服务来实现。

第二:在 Android 应用程序中,您需要使用 http post 或 get 与 Web 服务进行通信。

第三:从网络服务返回的响应应该由您的 Android 应用程序解析。解析器将读取响应并为您提供所需的字段值(有许多可用的解析包)

第四:获取值后,您可以选择将它们存储在首选项或 SQLite 表中(以供将来使用),或者使用 Intent 直接将它们发送到下一个 Activity。

掌握这项技术后,您将能够制作任何需要后端服务器的移动应用程序。

祝你好运

关于java - 如何将 EditText 中的 PHP 变量的值传递到 Android Studio?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49512968/

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