我是Android和PHP的新手。我要做的是从外部MySql数据库中将搜索值返回到Android设备。该数据库包含200,000条174MB的条目,因此将其包含在软件包中并使用内部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();
?>
在从布局调用的Test类中,我有这个。 问题是,我可以连接到外部数据库,甚至指定添加到数据库,但我在获取特定用户定义的术语时迷失了方向。现在,当我运行它时,它将尝试下载整个表(120,000个条目),解析为String,并显示到列表视图,但是在10MB时它崩溃并且必须是fc。如果我能弄清楚如何指定PHP和Android玩得很好,就像有人搜索鸡蛋一样,那么鸡蛋的所有值都会被返回(碳水化合物,卡路里,毫米等)。
由于PHP脚本是静态的,我不知道如何更改get命令,但我知道这是可能的,因为我之前已经看过应用程序。
package 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;
}
}
提前致谢。
P.S。我有正确的登录名,密码,数据库等。我已将其更改为帖子。
答案 0 :(得分: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
?>