gpt4 book ai didi

php - Android 外部在线数据库

转载 作者:行者123 更新时间:2023-11-30 00:34:16 25 4
gpt4 key购买 nike

我在 phpMyAdmin 中有一个在线数据库。此时我可以查询他,我可以在 TextView 中获取结果,但我想获取每个单元格的值,我该怎么办?

这是一个例子

[{"Monday":"","Tuesday":"","Wednesday":"","Thursday":"","Friday":"","Saturday":"","Sunday":"","id":"","info":""},...]

例如,我想获取星期一的值。

谢谢

代码sendQuery.java:

 public class sendQuery {
/////////// Public method to send Query ///////////
public static String send(String query) {
String result = "0";
InputStream is = null;
String weekDayVal;
//the query to send
ArrayList<NameValuePair> querySend = new ArrayList<NameValuePair>();

querySend.add(new BasicNameValuePair("querySend",query));

//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://locali.altervista.org/php/locali.php");
httppost.setEntity(new UrlEncodedFormEntity(querySend));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}

//convert response to string
try{
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();
try{
JSONArray weekDetails = new JSONArray ( result); // Your response string
for(int index=0;index < weekDetails.length();index++)
{
JSONObject tempWeekDetail = weekDetails.getJSONObject(index);
weekDayVal = tempWeekDetail.getString("Monday");// Value for Monday
//added this Log which you can view from LogCat. also changed above variable name
Log.i("Resp Value"," Moday Value"+weekDayVal);
}
}catch(Exception e)
{
//catch exception here
}
}catch(Exception e){
Log.e("log_tag", "Error converting result: "+e.toString());
}

Log.i("SendQUERY", result);
return result;
}

}

main.java:

 public class main extends Activity {
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView resLayout = (TextView) findViewById(R.id.res);


String res = sendQuery.send("SELECT * FROM contatti");

resLayout.append(res);


}
}

最佳答案

您可以尝试以下代码。

/*String res = "[{\"Monday\":\"Work\",\"Tuesday\":\"\",\"Wednesday\":\"\",\"Thursday\":\"\",\"Friday\":\"\",\"Saturday\":\"\",\"Sunday\":\"\",\"id\":\"\",\"info\":\"\"}]";//you have to escape double quotes*/

try{
JSONArray weekDetails = new JSONArray ( result); // Your response string
for(int index=0;index < weekDetails.length();index++)
{
JSONObject tempWeekDetail = weekDetails.getJSONObject(index);
String weekDayVal = tempWeekDetail.getString("Monday");// Value for Monday
//added this Log which you can view from LogCat. also changed above variable name
Log.i("Resp Value"," Moday Value"+weekDayVal);
}
}catch(Exception e)
{
//catch exception here
}

关于php - Android 外部在线数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22299278/

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