gpt4 book ai didi

php - Android HTTP POST 从 MySQL 数据库获取多个值

转载 作者:行者123 更新时间:2023-11-30 00:36:02 25 4
gpt4 key购买 nike

通过使用 HTTP POST 和 GET 方法,我可以获取单个数据,并且可以使用 MySQL 数据库插入多个数据。

现在我想从 MySQL db 获取多个值,并将其显示在 Android textView 上......我不知道如何将选择查询存储在数组中并在 Android 中获取此数组值

或者我必须使用 JSON..?

我的值.php。

  <?php
$con=mysqli_connect("localhost","arun","sachin11");
$db_select=mysqli_select_db($con,"Schoolapp");
if($db_select)
{
//echo "<br>db selected";
}
else
{
//echo "<br>db not exists";
}
//$ChildPassportname = $_GET['ChildPassportname'];
$query ="SELECT `Username`,`Password`,`Email`,`Gender` FROM `schooldb` WHERE ChildPassportName = 'arun2' ";
$result=mysqli_query($con,$query);

while($row = mysqli_fetch_array($result))
{
echo $row['Username'] . " " . $row['Password']. " " . $row['Email']. " " . $row['Gender'];
echo "<br>";
}

mysqli_close($con);
?>

MyJava 文件

package com.example.childprofile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ChildProfile extends Activity {
private TextView username,password,email,childid;
private Button get;
private static String name;
private EditText childname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child_profile);
username=(TextView) findViewById(R.id.profile_username);
password=(TextView) findViewById(R.id.profile_password);
email=(TextView) findViewById(R.id.profile_email);
childid=(TextView) findViewById(R.id.profile_childid);
get=(Button) findViewById(R.id.profile_button1);
childname=(EditText) findViewById(R.id.childname);
get.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Profile_getdata().execute();
}
});
}

public class Profile_getdata extends AsyncTask<String, Void, String>
{

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try
{
name=childname.getText().toString();
String link="http://192.168.1.22:81/arun/value.php?";
String data = URLEncoder.encode("ChildPassportname", "UTF-8")
+ "=" + URLEncoder.encode(name, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter
(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader
(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}

}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
username.setText(result[0]);
password.setText(result[1]);
email.setText(result[2]);
childid.setText(result[3]);
}

}

}

MyXml 文件

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1E8EB6">

<TextView
android:id="@+id/profile_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/profile_imageView1"
android:layout_marginBottom="20dp"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/profile_imageView1"
android:gravity="center"
android:text="Child Form"
android:textColor="#FFFFFF"
android:textSize="30sp" />

<ImageView
android:id="@+id/profile_imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/profile_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/profile_child"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"

android:textColor="#000000"
android:textSize="16sp"
android:background="#FFFFFF"
android:textStyle="bold|italic"
android:gravity="center"/>

<TextView
android:id="@+id/profile_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/profile_username"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:gravity="center"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold|italic" />

<TextView
android:id="@+id/profile_childid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/profile_password"
android:layout_marginTop="32dp"
android:background="#FFFFFF"
android:gravity="center"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold|italic" />

<TextView
android:id="@+id/profile_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/profile_childid"
android:layout_marginTop="28dp"
android:background="#FFFFFF"
android:gravity="center"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold|italic" />

<Button
android:id="@+id/profile_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:text="Get Child Data" />

<EditText
android:id="@+id/childname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/profile_button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:ems="10" />

</RelativeLayout>

最佳答案

了解 json 。它非常简单,最常用于发送和接收数据。几乎所有语言都支持它,或者相反。

关于php - Android HTTP POST 从 MySQL 数据库获取多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22164604/

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