连接到MySQL数据库

时间:2011-12-21 23:57:44

标签: android mysql database database-connection

我正在尝试连接到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进入历史功能,但按照降序排列,从最新的条目到最旧的。它看起来像我正确的方式吗?

1 个答案:

答案 0 :(得分:1)

您没有尝试连接到数据库。您正在尝试将数据发布到Web服务。如果您正在获得结果,那么您的客户端代码就可以了,并且您没有连接问题。

如果您的实际问题与拆分结果并将其放入不同位置有关,那么您必须告诉我们结果是什么样的。