android - 连接到 MySQL 数据库

标签 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 个进入历史功能,但按降序排列,从最新条目到最旧条目进行组织。我看起来处理这个问题的方式正确吗?

最佳答案

您没有尝试连接到数据库。您正在尝试将数据发布到网络服务。如果您得到结果,则说明您的客户端代码没有问题,并且连接没有问题。

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

关于android - 连接到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8597916/

相关文章:

mysql - #1054 - 'general' 中的未知列 'where clause'

php - Laravel 从数据库加载设置

python - 如何回滚循环django中的事务

android - 我如何在 Android 上获取 GPS 定位状态 - 现在(不使用监听器)

java - 从使用线程的另一个类检索 String 时出现问题

mysql - 我的mysql用户定义函数有什么错误?

php - MySQL 导出导入,外键 ID 自动递增

android - 从 android shell 向数据库中插入行的有效方法?

android - 使用相机拍照时如何找到 getThumbnail 的 origID.TakePicture

java - 无法从服务调用主 Activity