简体   繁体   English

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

[英]get list view with specific records from server based on email address sent from android

I have a table in the server and I want to retrieve specific records based on email address sent from android java code. 我在服务器中有一个表,我想根据从android java代码发送的电子邮件地址检索特定记录。

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 请帮我做,我尝试了3天,但是没有解决办法

this is my PHP code : 这是我的PHP代码:

<?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 : 这是我的Java代码:

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 + "\n"
                        + "Vehicle Model : " + Type1 + "\n" + "Service Type : " + Model1
                        + "\n" + "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 我只是将条件放在Java代码中,而PHP代码中没有任何变化

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM