Porque dá erro? [ReSoLvIdO]

4 respostas
pedroroxd

Pessoal, estou tentando conectar o celular Android com um BD que ta na net..
Tenho uma página php pra passar os dados.

Página php (getAllPeopleBornAfter):
<?php
			  mysql_connect("localhost","root","root");
			  mysql_select_db("androider_local");
			  
			  $q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
			  
			  while($e=mysql_fetch_assoc($q))
					  $output[]=$e;
			   
			  print(json_encode($output));
			  mysql_close();
      ?>

Eu consigo acessar essa página assim: http://localhost/getAllPeopleBornAfter.php, mas tem que passar o parâmetro (o ano)

Segue a classe que fiz com o Android:
public class activity extends Activity {
	TextView tvimc;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CarregaTelaPrincipal();
    }
    
	public void CarregaTelaPrincipal() {
		setContentView(R.layout.main);
		tvimc = (TextView) findViewById(R.field.lbinf);
		
		      String result = "";
		      //the year data to send
		      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		      nameValuePairs.add(new BasicNameValuePair("year","1980"));
		      InputStream is = null; 
		      
		      //http post
		      try{
		              HttpClient httpclient = new DefaultHttpClient();
		              HttpPost httppost = new HttpPost("http://localhost/getAllPeopleBornAfter.php");
		              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		              HttpResponse response = httpclient.execute(httppost);
		              HttpEntity entity = response.getEntity();
		              is = entity.getContent();
		      }catch(Exception e){
		              Log.e("log_tag", "Error in http connection "+e.toString());
		      }
		      //convert 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");
		              }
		              is.close();
		       
		              result=sb.toString();
		      }catch(Exception e){
		              Log.e("log_tag", "Error converting result "+e.toString());
		      }
		       
		      //parse 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")+
		                              ", birthyear: "+json_data.getInt("birthyear")
		                      );               
		              }
		              
		              
		      }catch(JSONException e){
		              Log.e("log_tag", "Error parsing data "+e.toString());
		      }
	}
}

Mas não ta funfando...
Onde to fazendo errado?
Vlws

4 Respostas

E

Você sabe o que é “localhost” ?

pedroroxd

que que tem o localhost?

pedroroxd

Eu consigo conectar com o banco de dados e exibir todos os dados com uma página PHP (To usando o WAMPSERVER)…
localhost / usuário = root / senha = root / BD = androider_local

Ae eu fiz essa página php… Eu consigo acessar assim: http://localhost/getAllPeopleBornAfter.php

Mas lógico que não vai ficar certo, porque ela precisa do parâmetro (ano), então sai assim:
Notice: Undefined index: year in C:\wamp\www\getAllPeopleBornAfter.php on line 5
[{“id”:“3”,“name”:“Pedro”,“sex”:“M”,“birthyear”:“1994”},{".

Não entendi o porque da pergunta do localhost… Não consigo fazer um HttpPost em um servidor local?

pedroroxd

Ja consegui, vlw ae pessoal.

Criado 5 de janeiro de 2011
Ultima resposta 6 de jan. de 2011
Respostas 4
Participantes 2