I’m new to Android and PHP. What I’m trying to do is retrieve the search value back to the Android device from an external MySql database. The database consists of 200,000 entries at 174MB, so including it in the package and using the internal SqlLite isn’t an option.
<?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();
?>
In the Test class which is called from a layout, I have this.
The problem is, I can connect to the external db, even specify adding to the database, but I’m lost on getting specific user defined terms. Right now when I run it, it will try to download the entire Table (120,000 entries), parse to a String, and show to a listview, but at 10MB it crashes and must be fc. If I could figure out how to specify the PHP and Android to play nice, like when someone searches for eggs, then all values for eggs are returned (carbs, calories, mm, etc).
Since the PHP script is static, I don’t see how it is possible to change the get command, but I know it is possible because I’ve seen apps before to do it.
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;
}
}
Thanks in advance.
p.s. I have the correct login, password, db, etc. I changed them for the post.
You’ll have to put a WHERE clause in your query.
Catch the Abbrev parameter sent to your script from Android like this…