Php Android HTTP POST从MySQL数据库获取多个值
通过使用httppost&GET方法,我可以获得单个数据,也可以使用MySQL数据库插入多个数据 现在我想从MySQL数据库中获取多个值,并将其显示在Android textView上。。。。 我不知道如何将select查询存储在数组中,并在Android中获取该数组值 或者我必须使用JSON myvalue.phpPhp Android HTTP POST从MySQL数据库获取多个值,php,android,mysql,Php,Android,Mysql,通过使用httppost&GET方法,我可以获得单个数据,也可以使用MySQL数据库插入多个数据 现在我想从MySQL数据库中获取多个值,并将其显示在Android textView上。。。。 我不知道如何将select查询存储在数组中,并在Android中获取该数组值 或者我必须使用JSON myvalue.php <?php $con=mysqli_connect("localhost","arun","sachin11"); $db_select=mysqli_s
<?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]);
}
}
}
package com.example.childprofile;
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.io.OutputStreamWriter;
导入java.net.URL;
导入java.net.URLConnection;
导入java.net.urlcoder;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类ChildProfile扩展活动{
私有文本视图用户名、密码、电子邮件、儿童ID;
私人按钮获取;
私有静态字符串名;
私有编辑文本子名称;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.child_profile);
用户名=(TextView)findViewById(R.id.profile\u用户名);
密码=(TextView)findViewById(R.id.profile\u密码);
电子邮件=(TextView)findViewById(R.id.profile\u电子邮件);
childid=(TextView)findViewById(R.id.profile\u childid);
get=(按钮)findViewById(R.id.profile_按钮1);
childname=(EditText)findViewById(R.id.childname);
get.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
新配置文件_getdata().execute();
}
});
}
公共类配置文件\u getdata扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
尝试
{
name=childname.getText().toString();
字符串链接=”http://192.168.1.22:81/arun/value.php?";
String data=URLEncoder.encode(“ChildPassportname”、“UTF-8”)
+“=”+URLEncoder.encode(名称,“UTF-8”);
URL=新的URL(链接);
URLConnection conn=url.openConnection();
连接设置输出(真);
OutputStreamWriter wr=新的OutputStreamWriter
(conn.getOutputStream());
wr.写入(数据);
wr.flush();
BufferedReader reader=新的BufferedReader
(新的InputStreamReader(conn.getInputStream());
StringBuilder sb=新的StringBuilder();
字符串行=null;
//读取服务器响应
而((line=reader.readLine())!=null)
{
某人附加(行);
打破
}
使某人返回字符串();
}捕获(例外e){
返回新字符串(“异常:+e.getMessage());
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
username.setText(结果[0]);
password.setText(结果[1]);
email.setText(结果[2]);
childid.setText(结果[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
。它非常简单,最常用于发送和接收数据。所有语言都以某种方式支持它 检查这个答案,将MySQL行转换为JSON。