根据android发送的电子邮件地址从服务器获取具有特定记录的列表视图

根据android发送的电子邮件地址从服务器获取具有特定记录的列表视图

问题描述:

I have a table in the server and I want to retrieve specific records based on email address sent from android java code.

The email address is stored in global variable and I can get its value but I don't know how to send its value to the server and get the records.

my code retrieves all the records from the table

Please Help me to do it, I tried to do it for 3 days but with no solution

this is my PHP code :

<?php
$objConnect = mysql_connect("****","******","******");
$objDB = mysql_select_db("******");
$strSQL = "SELECT * FROM Appointment ";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
    $arrCol = array();
    for($i=0;$i<$intNumField;$i++)
    {
        $arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
    }
    array_push($resultArray,$arrCol);
}

mysql_close($objConnect);

echo json_encode($resultArray);
?>    

this is my java code :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ServicesBMWGarageActivity extends Activity {

private ClipData myClip;

private ClipboardManager myClipboard;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.services_bmw_garage);

    GlobalClass globalVariable = (GlobalClass) getApplicationContext();
    // Get email from global/application context
    final String Email  = globalVariable.getEmail();

    myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

    // Permission StrictMode
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    // listView1
    final ListView lisView1 = (ListView)findViewById(R.id.listView1);   


    String url = "http://ec2-54-148-64-28.us-west-2.compute.amazonaws.com/Codiad/workspace/BMWdatabase/MyAppointments.php";

    try {

        JSONArray data = new JSONArray(getJSONUrl(url));

        final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);

            map = new HashMap<String, String>();
            map.put("A_ID", c.getString("A_ID"));
            map.put("A_Model", c.getString("A_Model"));
            map.put("A_Services", c.getString("A_Services"));
            map.put("A_DATE", c.getString("A_DATE"));
            MyArrList.add(map);

        }


        SimpleAdapter sAdap;
        sAdap = new SimpleAdapter(ServicesBMWGarageActivity.this, MyArrList, R.layout.activity_column,
                new String[] {"A_ID", "A_Model", "A_Services", "A_DATE"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel,R.id.Coldate });      
        lisView1.setAdapter(sAdap);

        final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);
        // OnClick Item
        lisView1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> myAdapter, View myView,
                    int position, long mylng) {

                String A_ID1 = MyArrList.get(position).get("A_ID")
                        .toString();
                String Type1 = MyArrList.get(position).get("A_Model")
                        .toString();
                String Model1 = MyArrList.get(position).get("A_Services")
                        .toString();
                String Model2 = MyArrList.get(position).get("A_DATE")
                        .toString();
                final String phoneNumber = MyArrList.get(position).get("A_ID")
                        .toString();
                //String sMemberID = ((TextView) myView.findViewById(R.id.ColMemberID)).getText().toString();
                // String sName = ((TextView) myView.findViewById(R.id.ColName)).getText().toString();
                // String sTel = ((TextView) myView.findViewById(R.id.ColTel)).getText().toString();

                viewDetail.setIcon(android.R.drawable.btn_star_big_on);
                viewDetail.setTitle("Appointment Detail");
                viewDetail.setMessage("ID : " + A_ID1 + "
"
                        + "Vehicle Model : " + Type1 + "
" + "Service Type : " + Model1
                        + "
" + "Date : " + Model2);
                viewDetail.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                String text = phoneNumber;
                                myClip = ClipData.newPlainText("text", text);
                                myClipboard.setPrimaryClip(myClip);
                                Toast.makeText(getApplicationContext(), "Appointment ID Copied", 
                                Toast.LENGTH_SHORT).show();

                                dialog.dismiss();
                            }
                        });
                viewDetail.show();

            }
        });


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


public String getJSONUrl(String url) {
    StringBuilder str = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) { // Download OK
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                str.append(line);
            }
        } else {
            Log.e("Log", "Failed to download results..");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return str.toString();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_activity_home_page, menu);
    return true;
}

}

I found the answer and the problem is solved

It was an easy solution

I just put my condition in the java code with no changes in the PHP code

the changes are :

try {
        JSONArray data = new JSONArray(getJSONUrl(url));

        final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String,    String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);
            map = new HashMap<String, String>();
            map.put("V_ID", c.getString("V_ID"));
            map.put("V_Type", c.getString("V_Type"));
            map.put("V_Model", c.getString("V_Model"));
            map.put("V_Year", c.getString("V_Year"));
            map.put("V_Plate", c.getString("V_Plate"));
            map.put("ImagePath", c.getString("ImagePath"));
            map.put("Email", c.getString("Email"));

            String email = c.getString("Email");
              GlobalClass globalVariable = (GlobalClass) getApplicationContext();
              // Get name and email from global/application context
              final String Email  = globalVariable.getEmail();

             if(email.equals(Email))
            MyArrList.add(map);
        }