SnakeSheet
SnakeSheet

Reputation: 151

Android isn't getting the correct query result from php

My android program isn't getting the information from my database well :/

This is what he should receive:

enter image description here

but he he's receiving this

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

This is my java code to send and receive:

// 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));

And this is the php file that is called in url_video_type:

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

What is wrong with my code ?

Upvotes: 0

Views: 48

Answers (1)

Hitesh
Hitesh

Reputation: 66

Please try to use mysql_fetch_array() and also try mysql_fetch_row() in place of mysql_fetch_assoc() . it may be work

Upvotes: 1

Related Questions