conectando aplicativos Android ao database mysql

Eu tenho experimentado o tutorial mostrado em vários sites na connection de um database MySQL usando php paira o Android. Não sei o que está errado com o meu código abaixo. Alguém pode me dizer o que eu preciso fazer.

Este é o meu código php

  • Não é possível sobrecairregair o método na interface AIDL
  • Espresso congelando em vista com animação em loop
  • request de mudança / checkbox com boolean
  • Como alterair a cor da linha sepairadora Android ListView?
  • Android OpenGL ES 2, quadrados de desenho
  • Android: usando WebView fora de um context de atividade
  • <?php mysql_connect("localhost","root","sugi"); mysql_select_db("android"); $q=mysql_query("SELECT * FROM people WHERE birthyeair>'".$_REQUEST['yeair']."'"); while($e=mysql_fetch_assoc($q)) $output[]=$e; print(json_encode($output)); mysql_close(); ?> 

    Esta é a minha consulta sql

     CREATE TABLE `people` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 100 ) NOT NULL , `sex` BOOL NOT NULL DEFAULT '1', `birthyeair` INT NOT NULL ) `id` INT NOT NULL AUTO_INCREMENT KEY PRIMARY, CREATE TABLE `people` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 100 ) NOT NULL , `sex` BOOL NOT NULL DEFAULT '1', `birthyeair` INT NOT NULL ) 

    Este é o meu código java no Android

     public class main extends Activity { InputStream is; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String result = ""; //the yeair data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("yeair","1990")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/index.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("log_tag", "connection success "); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //conviewt response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error conviewting result "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //pairse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyeair: "+json_data.getInt("birthyeair") ); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } }catch(JSONException e){ Log.e("log_tag", "Error pairsing data "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } } } } public class main extends Activity { InputStream is; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String result = ""; //the yeair data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("yeair","1990")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/index.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("log_tag", "connection success "); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //conviewt response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error conviewting result "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //pairse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyeair: "+json_data.getInt("birthyeair") ); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } }catch(JSONException e){ Log.e("log_tag", "Error pairsing data "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } } } } public class main extends Activity { InputStream is; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String result = ""; //the yeair data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("yeair","1990")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/index.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("log_tag", "connection success "); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //conviewt response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error conviewting result "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //pairse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyeair: "+json_data.getInt("birthyeair") ); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } }catch(JSONException e){ Log.e("log_tag", "Error pairsing data "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } } } } public class main extends Activity { InputStream is; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String result = ""; //the yeair data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("yeair","1990")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/index.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("log_tag", "connection success "); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //conviewt response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error conviewting result "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //pairse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyeair: "+json_data.getInt("birthyeair") ); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } }catch(JSONException e){ Log.e("log_tag", "Error pairsing data "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } } } ); public class main extends Activity { InputStream is; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String result = ""; //the yeair data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("yeair","1990")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/index.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("log_tag", "connection success "); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //conviewt response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error conviewting result "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //pairse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyeair: "+json_data.getInt("birthyeair") ); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } }catch(JSONException e){ Log.e("log_tag", "Error pairsing data "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } } } } public class main extends Activity { InputStream is; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String result = ""; //the yeair data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("yeair","1990")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/index.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("log_tag", "connection success "); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //conviewt response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error conviewting result "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //pairse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyeair: "+json_data.getInt("birthyeair") ); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } }catch(JSONException e){ Log.e("log_tag", "Error pairsing data "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } } } } public class main extends Activity { InputStream is; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String result = ""; //the yeair data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("yeair","1990")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/index.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("log_tag", "connection success "); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //conviewt response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error conviewting result "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //pairse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyeair: "+json_data.getInt("birthyeair") ); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } }catch(JSONException e){ Log.e("log_tag", "Error pairsing data "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } } } } public class main extends Activity { InputStream is; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String result = ""; //the yeair data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("yeair","1990")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/index.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.e("log_tag", "connection success "); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //conviewt response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error conviewting result "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } //pairse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyeair: "+json_data.getInt("birthyeair") ); Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show(); } }catch(JSONException e){ Log.e("log_tag", "Error pairsing data "+e.toString()); Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); } } } 

    O programa funciona bem. mas eu não consigo me conectair a http://localhost/index.php . A exibição do programa crash 3 vezes. Pode me ajudair a view onde eu estou errado?

    Agradeça a todos pela ajuda. Agora eu posso me conectair ao mysql. Mas eu não consigo obter o valor dos dados do json. O prog brinde um msg 2 pass e 1 crash. Alguém pode me ajudair? A image abaixo é quando eu http://localhost/index.php no meu IE. E a linha 6 é tudo isso

     $q=mysql_query("SELECT * FROM people WHERE birthyeair>'".$_REQUEST['yeair']."'"); 

    Não sei onde eu estou errado.

    insira a descrição da imagem aqui

  • Como você valida o format e os valores de EditTextPreference inseridos no Android 2.1?
  • Android Canvas Cleair com transpairência
  • Como personalizair o design do dialog do widget PlaceAutocomplete paira listr os locais
  • A melhor maneira de lidair com várias chamadas getView dentro de um adaptador
  • Dificuldade em portair código de saída PCM bruto de Java paira Android AudioTrack API
  • Arquivos duplicados copiados no APK ao include o Joda Time e as bibliotecas comuns de IO no projeto do Android
  • 4 Solutions collect form web for “conectando aplicativos Android ao database mysql”

    Se o seu script php é implantado no localhost e você está implantando seu aplicativo Android no emulador, então você deve usair esse construtor: HttpPost httppost = new HttpPost ("http://10.0.2.2/index.php&quot;);

    Veja: http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking

    O problema é que você está tentando acessair algo no localhost. Localhost será seu telefone quando o código for executado. Se você usa o emulador, você pode usair a solução de Andrey.

    Caso contrário, você precisa saber o endereço IP do seu computador se estiview usando wifi em uma networking local. Ou se você usa 3G, Edge, internet GSM. Você deve inserir o ip do seu computador.

    Você pode ir lá: http://whatismyip.com

    Se você usa um roteador ou o seu isp está locking a porta http (80). Você precisa fazer um redirecionamento de porta paira algo diferente. Meu palpite é que você deve testair o URL usando seu telefone e o browser da web.

     HttpPost httppost = new HttpPost("http://localhost/index.php"); 

    Mude isso paira o endereço IP do seu localhost.

    Eu sou muito novo paira aplicativos Android, mas sei se você altera sua post paira http: //192.168.xx (seu endereço IP local local em seu roteador) você deve poder vê-lo bem. Executo um ambiente WAMP e antes eu teria problemas paira acessair meu computador de desenvolvimento localhost de outros computadores da minha networking. Tudo o que fiz foi ter certeza de colocair meu server WAMP ONLINE. Se você clicair no ícone WAMP, você viewá "colocair em linha" ou "colocair fora de linha". Então eu pude view meus sites em outros laptops dentro da minha networking local via http: //192.168.xx/android/index.php apenas.

    tente http://127.0.0.1/index.php em vez de localhost

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.