我的android程序没有从我的数据库中获取信息:/
这是他应该收到的:
但是他接受了这个
{"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);
?>
我的代码出了什么问题?
答案 0 :(得分:1)
请尝试使用 mysql_fetch_array(),并尝试使用 mysql_fetch_row()代替mysql_fetch_assoc()。它可能是工作