I have the following code in php:
<?php
try {
$gbd = new PDO('mysql:host=localhost;dbname=tramites', $username,$password);
$categoria = $_POST['categoria'];
$stmt = $gbd->prepare("SELECT nombre, categoria FROM tramite WHERE categoria = :categ");
$result = $stmt->execute(array("categ"=>$categoria));
$i=0;
$arreglo = array(); // arreglo para enviar
$result2 = $stmt->fetchAll();
foreach ($result2 as $row)
{
$arreglo[$i] = $row['nombre'].', '.$row['categoria'];
$i++;
}
$gbd = null;
}
catch (PDOException $e)
{
print "¡Error!: " . $e->getMessage() . "<br/>";
die();
}
if($i >= 0)
{
echo json_encode($arreglo);
}
?>
this code i´ts called from an android app and I need to receive the arreglo
array in android but it returns null
I don't
know why.
My main code:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings2) // JSON
{
String n="";
HttpHandler handler= new HttpHandler();
String txt=handler.getTramites2("tramites");
n = parseProfilesJson(txt);
Toast.makeText(this, "ca / "+ n+" / "+ txt, Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
public String parseProfilesJson(String the_json)
{
String res="";
try {
JSONObject myjson = new JSONObject(the_json);
JSONArray nameArray = myjson.names();
JSONArray valArray = myjson.toJSONArray(nameArray);
for(int i=0;i<valArray.length();i++)
{
String p = nameArray.getString(i) + "," + valArray.getString(i);
res = res + p;
//Toast.makeText(this, "ca "+ n, Toast.LENGTH_LONG).show();
}
} catch (JSONException e)
{ e.printStackTrace(); }
return res;
}
and the class that is the HttpHandler:
public class HttpHandler
{
protected String url = "http://192.168.43.98/tramitapue/";
static String json = "";
public String getTramites2(String categoria)
{
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url+"getTramites2.php");
//Parámetros
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("categoria", categoria));
// envio los parametros
httppost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse resp = httpclient.execute(httppost);
// recibo paramteros
//HttpEntity ent = resp.getEntity();
////////////////////////////////////
// here i put json content
// trace response
InputStream is = resp.getEntity().getContent();
//convert response to string
BufferedReader myReader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
StringBuilder sb = new StringBuilder();
sb.append(myReader.readLine() + "\n");
String line="";
while ((line = myReader.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
////////////////////////////////////
//String text = EntityUtils.toString(ent);
return json;
}catch(Exception e){ return "Error en la Conexión " + e;}
}
I don't get any error but I get a null value. If I try in php only with a string it works but I want an array. What´s wrong. I apologize in advance cause my variables are a mix beetwen spanish and english.