gpt4 book ai didi

php - Android 无法从 php 获取正确的查询结果

转载 作者:行者123 更新时间:2023-11-29 12:54:01 24 4
gpt4 key购买 nike

我的 Android 程序无法很好地从我的数据库获取信息:/

这是他应该收到的:

enter image description here

但是他收到了这个

{"entradas":[{"cnt":"2","ent_video_tipo":"1"},{"cnt":"2","ent_video_tipo":"2"},{"cnt":"2","ent_video_tipo":"3"}]}

这是我要发送和接收的java代码:

// Async Task to access the web
private class GetVideoType extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
httppost.setEntity(new UrlEncodedFormEntity(params_aux));

HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
}

catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}

catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}

@Override
protected void onPostExecute(String result) {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("entradas");
video_tipo_sort = new int [jsonMainNode.length()-1];

for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
// Retrive JSON Objects
video_tipo_sort[i] = Integer.parseInt(jsonChildNode.getString("ent_video_tipo"));

}
} catch (JSONException e) {
e.printStackTrace();
}
}
}// end async task

public void accessWebServiceGetVideoType() {
GetVideoType task = new GetVideoType();
// passes values for the urls string array
task.execute(new String[] { url_video_type });
}

params_aux:

List<NameValuePair> params_aux = new ArrayList<NameValuePair>();
params_aux.add(new BasicNameValuePair("id", id));

这是在 url_video_type 中调用的 php 文件:

<?php
$host="www.example.com";
$username="user";
$password="pass";
$db_name="table";

$user_id = $_POST['id'];

$bd=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$q = "SELECT ent_video_tipo, count(ent_video_tipo) as cnt
FROM entradas
WHERE entradas.ent_user = $user_id
GROUP BY ent_video_tipo
ORDER BY cnt DESC";
$result = mysql_query($q);
$json = array();

if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['entradas'][]=$row;
}
}
mysql_close($bd);
echo json_encode($json);
?>

我的代码有什么问题?

最佳答案

请尝试使用 mysql_fetch_array() 并尝试 mysql_fetch_row() 代替 mysql_fetch_assoc() 。这可能是工作

关于php - Android 无法从 php 获取正确的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24348221/

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