douxia9826 2012-01-10 07:44
浏览 32
已采纳

Android - 没有通过php存储到MySQL数据库中的值

I am sending some values through HTTP Post to my server from my Android emulator but the values are not being stored. My logcat is showing response code 200 and displaying the codes of the php script as a http response. My database is fine as i am able to insert data in it. Any idea what might be the matter?

main.java

public void sendRegistrationIdToServer(String deviceId,
        String registrationId) {
    System.out.println(registrationId);
    System.out.println(deviceId);

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://10.21.78.11/storePost.php?");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        // Get the deviceID
        nameValuePairs.add(new BasicNameValuePair("devid", deviceId));
        nameValuePairs.add(new BasicNameValuePair("regid",
                registrationId));
        //HttpProtocolParams.setUseExpectContinue(client.getParams(), false);
        post.addHeader("Content-Type", "application/x-www-form-urlencoded");
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));

        // Execute HTTP Post Request
        HttpResponse response = client.execute(post);
        int status = response.getStatusLine().getStatusCode();
        System.out.println("HTTP Status = "+status);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));

        String line = "";
        while ((line = rd.readLine()) != null) {
            Log.e("HttpResponse", line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

storePost.php

<?php
mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("DeviceID");
$regid = mysql_real_escape_string($_POST["regid"]);
$devid = mysql_real_escape_string($_POST["devid"]);
mysql_query("INSERT INTO Android(regID, devID) VALUES ('$regid', '$devid')") or die(mysql_error());
mysql_close();
?>
  • 写回答

1条回答 默认 最新

  • duanbage2161 2012-01-10 08:29
    关注

    Make sure your web server is configured to run php codes. If you go to storePost.php with your desktop browser and you still see the codes displayed in the browser, then it's a configuration issue with your webserver. Until you fix that, the Android code will still be returning php codes as a response.

    If you're on a windows platform, you might want to check this out http://www.simplehelp.net/2008/08/25/how-to-install-and-setup-apache-mysql-and-php-in-windows/

    or linux https://help.ubuntu.com/community/ApacheMySQLPHP

    Even easier, check xampp out www.apachefriends.org/en/xampp.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi
  • ¥15 ue2.6.12版本用的若以,安装gojs,引入import * as go from 'gojs';报错