从 Android 应用程序发送参数到服务器

发布于 2024-12-10 02:12:33 字数 2285 浏览 0 评论 0原文

我有一个 Android 应用程序,它从我的 mysql 数据库请求数据。这工作正常,但是当我尝试发送要检索的项目数的参数时,我什么也得不到。

这是我的 java 代码:

        result = "";
        client = new DefaultHttpClient();
        post = new HttpPost("http://www.XXX.XXXX.XX/XX.php");

        nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("itemsToGet", itemsToGet));

        try {
            post.setEntity((new UrlEncodedFormEntity(nameValuePairs)));
            response = client.execute(post);
            entity = response.getEntity();
        inputStream = entity.getContent();
    } catch (UnsupportedEncodingException e) {
            Log.e(TAG, e.toString());
        } catch (ClientProtocolException e) {
            Log.e(TAG, e.toString());
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }

        try {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream ,"iso-8859-1"), 8);
            stringBuilder = new StringBuilder();
            String line = null;

            while((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }
            inputStream.close();

            result = stringBuilder.toString();

            } catch (UnsupportedEncodingException e) {
            Log.e(TAG, e.toString());
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }

        try {
            JSONArray jsonArray = new JSONArray(result);

            for(int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                Log.i(TAG, object.getString("namn") + " " + i);
        }   
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
    }

这是我的服务器端 PHP 代码:

<?php
mysql_connect("XXX.XXX.com", "XXX", "XXX") or die(mysql_error());
mysql_select_db("XXXXXXX") or die(mysql_error());

$data = mysql_query("SELECT * FROM artikel") or die(mysql_error());
$itemsToGet = intval($_POST['itemsToGet']);
$counter = 0;

while($info = mysql_fetch_array( $data ) && $counter < $itemsToGet)
{
    $databaseInfo[] = $info;
    $counter++;
}

print(json_encode($databaseInfo));

?>

I have an android application that requests data from my mysql database. This works fine but when I try to send a parameter for how many items to retrieve I get nothing.

This is my java code:

        result = "";
        client = new DefaultHttpClient();
        post = new HttpPost("http://www.XXX.XXXX.XX/XX.php");

        nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("itemsToGet", itemsToGet));

        try {
            post.setEntity((new UrlEncodedFormEntity(nameValuePairs)));
            response = client.execute(post);
            entity = response.getEntity();
        inputStream = entity.getContent();
    } catch (UnsupportedEncodingException e) {
            Log.e(TAG, e.toString());
        } catch (ClientProtocolException e) {
            Log.e(TAG, e.toString());
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }

        try {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream ,"iso-8859-1"), 8);
            stringBuilder = new StringBuilder();
            String line = null;

            while((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + "\n");
            }
            inputStream.close();

            result = stringBuilder.toString();

            } catch (UnsupportedEncodingException e) {
            Log.e(TAG, e.toString());
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }

        try {
            JSONArray jsonArray = new JSONArray(result);

            for(int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                Log.i(TAG, object.getString("namn") + " " + i);
        }   
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
    }

And this is my server side PHP code:

<?php
mysql_connect("XXX.XXX.com", "XXX", "XXX") or die(mysql_error());
mysql_select_db("XXXXXXX") or die(mysql_error());

$data = mysql_query("SELECT * FROM artikel") or die(mysql_error());
$itemsToGet = intval($_POST['itemsToGet']);
$counter = 0;

while($info = mysql_fetch_array( $data ) && $counter < $itemsToGet)
{
    $databaseInfo[] = $info;
    $counter++;
}

print(json_encode($databaseInfo));

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

享受孤独 2024-12-17 02:12:33

我解决了。 while 循环中的条件不正确。

while($info = mysql_fetch_array( $data ) && $counter < $itemsToGet)

应该是

while(($info = mysql_fetch_array( $data )) && ($counter < $itemsToGet))

I solved it. The condition in the while-loop was incorrect.

while($info = mysql_fetch_array( $data ) && $counter < $itemsToGet)

It should be

while(($info = mysql_fetch_array( $data )) && ($counter < $itemsToGet))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文