Note that there are some explanatory texts on larger screens.

plurals
  1. POConnect to a MySQL database
    primarykey
    data
    text
    <p>I'm trying to connect to a MySQL database to display some information in a TextView. I've been trying to by this tutorial, but I have been struggling. My PHP should be fine. My concern is with handling the data. I'm more familiar with C# than Java so that may be where my confusion begins.</p> <p>Here is what I have so far:</p> <pre><code> package shc_BalloonSat.namespace; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; 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.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; //import android.view.View; import org.json.JSONArray; import org.json.JSONException; public class Shc_BalloonSat_Activity extends Activity { int historyCountFromUser; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); assignInfoToInfoTextView(); assignInfoToHistoryTextView(); } public void assignInfoToInfoTextView() { connect2DB(); JSONArray jArray = new JSONArray(result); TextView infoTV = (TextView)this.findViewById(R.id.info); String infoText = "Last Known Altitude: " + /*put db info here*/ /*+*/ "\n"; infoText += "Last Known Speed: " + /*put db info here*/ /*+*/ "\n"; infoText += "Last Known Latitude:" + /*put db info here*/ /*+*/ "\n"; infoText += "Last Known Longtitude: " + /*put db info here*/ /*+*/ "\n"; infoTV.setText(infoText); } public void assignInfoToHistoryTextView() { connect2DB(); JSONArray jArray = new JSONArray(result); for (int count = 0; count &lt;= 4; count++) { TextView historyTV = (TextView)this.findViewById(R.id.history); String historyText = "Altitude: " + /*put db info here*/ /*+*/ "\n"; historyText += "Speed: " + /*put db info here*/ /*+*/ "\n"; historyText += "Latitude:" + /*put db info here*/ /*+*/ "\n"; historyText += "Longtitude: " + /*put db info here*/ /*+*/ "\n\n"; historyTV.append(historyText); } } @SuppressWarnings("null") public void connect2DB() { String result = ""; ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); // Get data from database using HTTP Post try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("\* link to php file goes here */"); httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = new response.getEntity(); InputStream is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in HTTP connection "); } // Convert the data to a string that the phone can read try { InputStream is = null; 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 "); } // Parse data catch (JSONException e) { Log.e("log_tag", "Error parsing data "+e.toString()); } } private HttpEntity newUrlEncodedFormEntity( ArrayList&lt;NameValuePair&gt; nameValuePairs) { // TODO Auto-generated method stub return null; } } </code></pre> <p>My query pulls the last 5 entries from the database. I'm wanting the very last one to go in the regular funcition and all the other 4 to go in the history function but in a descending order that way they are organized from newest entry to oldest. Does it look like I'm going about this the right way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload