gpt4 book ai didi

android - 连接到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 14:37:59 26 4
gpt4 key购买 nike

我正在尝试连接到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com