Dúvida ... Php para servlet

0 respostas
pedroroxd
Pessoal, tenho uma aplicação Android, que pega coisas do banco de dados assim:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("pais", pais));
		nameValuePairs.add(new BasicNameValuePair("estado", estado));
		nameValuePairs.add(new BasicNameValuePair("cidade", cidade));

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost("http://192.168.0.103/gettudo.php"); //site que pego as coisas
		httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		HttpResponse response = httpclient.execute(httppost);
		HttpEntity entity = response.getEntity();
		is = entity.getContent();

		//E depois tenho um JSONArray pra fazer a varredura
			JSONArray jArray = new JSONArray(result);
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject json_data = jArray.getJSONObject(i);
				String pais = json_data.getString("pais"); //pegando o pais
E tenho o arquivo php assim:
<?php
			  mysql_connect("localhost","root","root");
			  mysql_select_db("androider_local");
			  
			  $q=mysql_query("SELECT * FROM remedios WHERE pais = '".$_REQUEST['pais']."' && estado = '".$_REQUEST['estado']."' && cidade = '".$_REQUEST['cidade']."' ");

			  while($e=mysql_fetch_assoc($q))
					  $output[]=$e;
			   
			  print(json_encode($output));
			  mysql_close();
      ?>

Decidi que vou fazer tudo em java, então resolvi passar essa parte PHP para Servlet...
Fiz assim:

public class GetTudoServlet extends HttpServlet {

	private Connection connection;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		connection = new ConnectionFactory().getConnection();

		String sql = "";

		// buscando os parâmetros no request
		String req = request.getParameter("pegar");
		if (req == "pais") {
			sql = "SELECT * FROM remedios";
		} else if (req == "estado") {
			sql = "SELECT * FROM remedios where pais =" + request.getParameter("pegaest");
		} else if (req == "cidade") {
			sql = "SELECT * FROM remedios where pais =" + request.getParameter("pegaest") + "&& estado =" + request.getParameter("pegacity");
		}

		try {
			PreparedStatement stmt = connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			//TEM ALGO AKI??
			while (rs.next()) {
			}

			rs.close();
			stmt.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}
Agora como que faço akela parte do
while($e=mysql_fetch_assoc($q))
					  $output[]=$e;
			   
			  print(json_encode($output));
			  mysql_close();
?

Vlws

Criado 8 de janeiro de 2011
Respostas 0
Participantes 1