如何使用httpClient和JSON接收从php到android的数组?

如何使用httpClient和JSON接收从php到android的数组?,php,android,mysql,json,apache-httpclient-4.x,Php,Android,Mysql,Json,Apache Httpclient 4.x,我有以下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-&g

我有以下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);
}
?>
以及作为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;}

}
公共类HttpHandler
{
受保护的字符串url=”http://192.168.43.98/tramitapue/";
静态字符串json=“”;
公共字符串GetTramite2(字符串类别A)
{
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost-HttpPost=newhttppost(url+“getTramites2.php”);
//帕拉地铁
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“categoria”,categoria));
//帕拉梅特罗斯环境酒店
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse resp=httpclient.execute(httppost);
//累西波参数
//HttpEntity ent=resp.getEntity();
////////////////////////////////////
//这里我把json内容放在这里
//跟踪响应
InputStream=resp.getEntity().getContent();
//将响应转换为字符串
BufferedReader myReader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”);
StringBuilder sb=新的StringBuilder();
sb.append(myReader.readLine()+“\n”);
字符串行=”;
而((line=myReader.readLine())!=null){
sb.追加(第+行“\n”);
}
json=sb.toString();
////////////////////////////////////
//String text=EntityUtils.toString(ent);
返回json;
}catch(异常e){return“Error en la Conexión”+e;}
}

我没有收到任何错误,但得到了一个空值。如果我只使用字符串在php中尝试,它可以工作,但我需要一个数组。怎么了。我提前道歉,因为我的变量是西班牙语和英语的混合体。

您可以使用JSON对查询结果进行编码,并在应用程序中解析它们

也许这会帮助你:
您需要使用Android-Json-PHP和PHP-Json-Android


注意:您的代码似乎容易受到攻击。
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;}

}