0

By using HTTP POST & GET method,I can be able to get single data and can be able to insert multiple data using MySQL database.

Now I want to get multiple values from MySQL db ,and display it on Android textView.... I don't know that how to store the select query in array and get this array values in Android

Or I have to use JSON..?

My value.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 file

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 File

<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>
Grant
  • 2,413
  • 2
  • 30
  • 41

1 Answers1

0

read about json . its very simple and most commonly used for to send and receive data. All most all languages supports it in or the other way around.

Sush
  • 3,864
  • 2
  • 17
  • 35