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

标签 php android mysql

通过使用 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/

相关文章:

php - JWPlayer 和保护视频

php - json_encode(array) 插入数据库

android - 计费客户端 : In-app billing API version 3 is not supported on this device

android - 更新状态并更改其在 Facebook 上的可见性?

mysql - 从数据库中获取所有订单 - 只返回一行

mysql - 选择包含一个或多个完全大写的单词的记录

php - 从登录 SQL 表中获取用户 ID

php - doctrine 中的子查询计数 - querybuilder

android - 原生扫描二维码功能

mysql - 如何并行写入 MySQL 中的同一行?