如何从PHP检索数据并放入android微调器?

时间:2012-02-09 20:27:11

标签: android

我是新手。现在我面对这样的问题。

我正在使用CustomHttpClient连接到本地服务器。我想知道一个微调器如何显示来自php的价值以及它将如何工作?我不知道java代码?可以为我推荐一些示例或网站吗?

2 个答案:

答案 0 :(得分:0)

php服务需要返回json / xml数据。假设json数据,此链接可以帮助您how to do

答案 1 :(得分:0)

这是我使用的方法。注意我在这里添加了诸如网页地址(WebPage)之类的内容,因此您可以更轻松地查看正在发生的事情。我建议不要把它放在这里,因为如果主机地址发生变化,你将不得不返回并在你的代码中更改它。

Android方面:

/**
 * Class for receiving data from server.
 * @author Jonathan B
 *
 */
public class ReceiveData 
{
//FILL IN webPage with link to the All folder on your server.
private String webPage = "http://www.example.com/Receive/";

//File name of the PHP file that is to be used by the method.
private String fileName; 
String delims = " ,\t\n\"\\;{}[]\\()<>&^%$@!+/*~=";

/**
 * Constructor.
 */
public ReceiveData()
{

}

/**
 * Pass arguments to PHP file. Then receive data from PHP file.
 * @param file - PHP file in question
 * @param data - Arguments to pass to PHP file.
 * @return output - The data received from server.
 */
private ArrayList<String> receiveData(String file, ArrayList<NameValuePair> data)
{
    InputStream is = null;
    ArrayList<String> output = new ArrayList<String>();
    String line = null;

    //Connect and obtain data.
    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(webPage+file);
        httppost.setEntity(new UrlEncodedFormEntity(data));
        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());
    }

    //Parse data into ArrayList and return.
    try
    {
        BufferedReader reader = 
            new BufferedReader(new InputStreamReader(is,"iso-8859-1"));

        while ((line = reader.readLine()) != null) 
        {
            //Parse data into tokens and removing unimportant tokens.
            StringTokenizer st = new StringTokenizer(line, delims, false);

            while(st.hasMoreTokens())
            {
                String token = st.nextToken();
                output.add(token);
            }
        }

        is.close();
        //Log output of data in LogCat.
        Log.d("DATA","DATA:"+output);

    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error converting result "+e.toString());
    }
    return output;
}

/**
 * Gets all  data from GetAllData.php
 * @return output - ArrayList containing  data.
 */
public ArrayList<String> getData(String row)
{
    fileName = "GetData.php";

    //Add arguments to arrayList<NameValuePairs> so we can encode the data and send it on.
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("row", row));

    ArrayList<String> output = this.receiveData(fileName, nameValuePairs);
    return output;
}

现在对于php方面的东西,我在接收文件夹中有一个名为GetData.php的文件,如下所示:

<?php
/*
 * What this file does is it:
 * 1) Creates connection to database.
 * 2) Gets data from database.
 * 3) Encodes data to JSON. So this data can then be used by Android Application.
 * 4) Close database connection.
 */
require_once 'Connection.php';
require_once 'ReceiveAPI.php';

$server = new Connection();
$receive = new ReceiveAPI();

//Retrieve information.
$row = $_POST['row'];

//Connect to database.
$server->connectDB();
$output = $receive->getAllData($row);
print(json_encode($output));

//Disconnect from database.
$server->disconnectDB();
?>