繁体   English   中英

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

[英]Android isn't getting the correct query result from php

我的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); 
?> 

我的代码有什么问题?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM