2014-03-04 56 views
0

通過使用HTTP POST & GET方法,我可以獲得單個數據並且能夠使用MySQL數據庫插入多個數據。Android HTTP POST從MySQL數據庫中獲取多個值

現在我想從MySQL數據庫多個值,並將其顯示在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); 
    ?> 

米亞瓦文件

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> 
+0

檢查此答案http://stackoverflow.com/a/383664/1756198將MySQL行轉換爲JSON。 –

回答

0

瞭解json。其非常簡單並且最常用於發送和接收數據。大多數所有的語言都支持它或以其他方式支持它。