2014-06-22 43 views
0

我的Android程序沒有從我的數據庫獲取信息以及正確的查詢結果:/Android是沒有得到從PHP

這是他應該得到什麼:

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

什麼是錯我的代碼?

+1

順便說一句:代碼打開SQL注入:http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php –

+0

mysql功能已棄用,因爲PHP 5.5.0並將從未來版本中刪除[(source)](http://www.php.net/manual/en/intro.mysql.php)改爲使用mysqli並使用準備好的語句來避免sql注入[documentation] (http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) – TmKVU

+0

所以你建議我使用mysqli而不是mysql並使用預準備語句。對 ? – SnakeSheet

回答

1

請儘量使用mysql_fetch_array(),也嘗試在地方mysql_fetch_assoc的和mysql_fetch_row()()。它可能是工作