2

我是 Android 和 PHP 的新手。我要做的是从外部 MySql 数据库将搜索值检索回 Android 设备。该数据库由 174MB 的 200,000 个条目组成,因此不能将其包含在包中并使用内部 SqlLite。

<?php

  mysql_connect("myhost","myuser","mypass");

  mysql_select_db("mydb");

$sql=mysql_query("select * from myTable");

$output = array();
while($row = mysql_fetch_assoc($sql)) {
$output['txt'][] = $row;
}

exit (json_encode($output));

mysql_close();
?>

在从布局调用的测试类中,我有这个。问题是,我可以连接到外部数据库,甚至可以指定添加到数据库,但是我在获取特定的用户定义术语时迷失了方向。现在当我运行它时,它会尝试下载整个表(120,000 个条目),解析为字符串,并显示到列表视图,但在 10MB 时它会崩溃并且必须是 fc。如果我能弄清楚如何指定 PHP 和 Android 玩得很好,比如当有人搜索鸡蛋时,那么会返回鸡蛋的所有值(碳水化合物、卡路里、毫米等)。

由于 PHP 脚本是静态的,我看不到如何更改 get 命令,但我知道这是可能的,因为我以前见过应用程序来执行此操作。

包carbcounter.project;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;


public class SqlTest extends Activity {
/** Called when the activity is first created. */

   TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqltest);      
    LinearLayout rootLayout = new LinearLayout(getApplicationContext());  
    txt = new TextView(getApplicationContext());  
    rootLayout.addView(txt);  
    setContentView(rootLayout);  

    txt.setText("Connecting..."); 
    txt.setText(getServerData(KEY_121)); 



}
public static final String KEY_121 = "http://android.mywebspace.com/androidscript.php";



private String getServerData(String returnString) {

   InputStream is = null;

   String result = "";
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("Abbrev","eggs"));

    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(KEY_121);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }
    catch(Exception e)
    {
            Log.e("log_tag", "Error in http connection "+e.toString());
    }

    try
    {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) 
            {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }
    catch(Exception e)
    {
            Log.e("log_tag", "Error converting result "+e.toString());
    }
    try
    {
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","id: "+json_data.getInt("id")+
                            ", type: "+json_data.getString("type")+
                            ", description: "+json_data.getInt("description")+
                            ", carbs: "+json_data.getInt("carbs")
                    );
                    returnString += "\n\t" + jArray.getJSONObject(i); 
            }
    }
    catch(JSONException e)
    {
            Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return returnString; 
}    

}

提前致谢。

ps 我有正确的登录名、密码、数据库等。我为帖子更改了它们。

4

1 回答 1

2

您必须在查询中添加 WHERE 子句。

像这样捕获从 Android 发送到您的脚本的Abbrev参数......

<?php
    $abbrev = mysql_real_escape_string($_POST['Abbrev']);
    $query = sprintf("SELECT * FROM myTable WHERE Abbrev='%s'",$abbrev); //I assumed here that your table field is called Abbrev too
    ... your code here

?>
于 2012-07-02T09:49:23.807 回答