简体   繁体   中英

How to get JSON array fron php server using Android Asynchronous Http Client

I tried to get json data from server.

I tested php file and it works fine. (I can see json type's data with chrome)

here is my code

Thanks in advance,

//php
$result = mysql_query($sql, $connect);
if($result) {
    $rows = array();
    while($r = mysql_fetch_assoc($result)) {
        $rows[] = $r;
    }

    echo json_encode($rows);
}
else{
    echo false;
}
mysql_close($connect);

and here is java

public static JSONArray post(String url, RequestParams params) {
    client.post(getAbsoluteUrl(url), params, new JsonHttpResponseHandler(){
    @Override
    public void onSuccess(JSONArray data) {
            // TODO Auto-generated method stub
              HTTPRESPONSE = data;
              Log.v("OWL", "GOT JSON FROM php");
        }
    });
    return HTTPRESPONSE;
}

The client.post call seems to be asynchronous, so the "return HTTPRESPONSE" will happen BEFORE the data is returned and set.

you should provide a callback function or interface to the main post function, and call it in the onSuccess call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM