httpPost不会填写列表视图

时间:2014-12-04 23:10:30

标签: android mysql json

我有这个类用于HttpPost方法:

public class httpHandler {

public String post(String posturl, String where){

  try {

      HttpClient httpclient = new DefaultHttpClient();

      HttpPost httppost = new HttpPost(posturl);

      //AÑADIR PARAMETROS
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      params.add(new BasicNameValuePair("data",where));

       httppost.setEntity(new UrlEncodedFormEntity(params));

              /*Finalmente ejecutamos enviando la info al server*/
      HttpResponse resp = httpclient.execute(httppost);
      HttpEntity ent = resp.getEntity();/*y obtenemos una respuesta*/

      String text = EntityUtils.toString(ent);

      return text;

      }
      catch(Exception e) { 
      e.printStackTrace();
      return "error";}

}

}

然后,在asyntask中执行此类并使用该httpost填充listview:

class AsyncExecute extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
         try {
                // Llamamos al servicio web para recuperar los datos
           httpHandler handler = new httpHandler();
                String txt = handler.post("url", "pedro");  
                JSONObject jsonObject = new JSONObject(txt);
                JSONArray cities = jsonObject.getJSONArray("cities");

                // Recorremos el array con los elementos cities
                for(int i = 0; i < cities.length(); i++) {
                  JSONObject city = cities.getJSONObject(i);
                  // Creamos el objeto City
                  City c = new City(city.getString("name"),   
                 city.getInt("nametwo"),city.getString("posicion") );
                  c.setData(city.getString("photo"));


                  listaPersonas.add(c.photo);
                  // Almacenamos el objeto en el array que hemos creado anteriormente
                  citiesAvaiable.add(c);

                }
              }
              catch(Exception e) {
                e.printStackTrace();
              }
        return null;
    }
    protected void onPostExecute(Void result) {
        // Creamos el objeto CityAdapter y lo asignamos al ListView 

         CityAdapter cityAdapter = new CityAdapter(MainActivity.this, citiesAvaiable);
         lvCities.setAdapter(cityAdapter);

        super.onPostExecute(result);
    }

然后我在按钮点击中执行asynctask,但列表视图没有填充。

json php是:

<?php  

$con = mysql_connect('mysql.hostinger.es', 'u453215752_aitor', '*****');  
 mysql_query("SET CHARACTER SET utf8");  
mysql_query("SET NAMES utf8");  
$dato = $_POST['data'];
$cities['cities'] = array();

if( $con )  
{  
mysql_select_db('u453215752_droid');  

$res = mysql_query('select name, nametwo, photo, posicion from cities where  
name='$dato'');

while( $row = mysql_fetch_array($res) ) {
array_push($cities['cities'], array('posicion' => $row['posicion'], 'name' =>  
$row['name'], 'nametwo' => $row['nametwo'], 'photo' => base64_encode($row['photo'])));
}
mysql_free_result($res);
mysql_close($con);
}

header('Content-type: application/json');
echo json_encode($cities);

1 个答案:

答案 0 :(得分:1)

您同时拥有与同一网址的POST和GET连接。这是没有意义的。 php脚本只能处理POST。并且您的Android代码注意到收到的json文本txt在收到帖子后从php脚本发送。

完全删除httpget。只有POST。使用txt而非aux

你只需要:

txt = handler.post("http://.......php", "pedro"); 
if ( txt.equals("error" ) )
    return null;
if ( txt.startsWith("<br" ) )
    return null;

JSONObject jsonObject = new JSONObject(txt);

更改

$res = mysql_query('select name, nametwo, photo, posicion from cities where  
name='$dato'');

其中一个

$query = "select name, nametwo, photo, posicion from cities where name='$dato'";    
$query = "select name, nametwo, photo, posicion from cities where name=\'$dato\'";    
$query = "select name, nametwo, photo, posicion from cities where name=$dato";

$res = mysql_query($query);