Я пытаюсь подключиться к базе данных MySQL для отображения некоторой информации в TextView. Я пытался с помощью этого урока, но я изо всех сил. Мой PHP должен быть в порядке. Мое беспокойство связано с обработкой данных. Я больше знаком с C #, чем с Java, так что, возможно, именно здесь начинается мое замешательство.
Вот что у меня есть:
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 <= 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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// 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<NameValuePair> nameValuePairs)
{
// TODO Auto-generated method stub
return null;
}
}
Мой запрос извлекает последние 5 записей из базы данных. Я хочу, чтобы самый последний входил в обычную функцию, а все остальные 4 - в функцию истории, но в порядке убывания, чтобы они были организованы от самой новой записи до самой старой. Похоже, я иду по этому пути?