douliao7354 2015-07-06 11:35 采纳率: 0%
浏览 751

Android Studio 将数据发送到 MySql 数据库的问题

我想从我的 android 应用程序中保存一些数据到服务器上的 mysql 数据库中。问题是一切看起来都很完美,我得到的信息是我的数据已经成功发送,但是我的数据库没有收到任何信息。我有以下文件:

main.java
  // setup a dialog window
        alertDialogBuilder.setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        // Drawing the marker in the Google Maps
                        drawMarker(point);
                        // Drawing the circle
                        drawCircle(point);
                        //Add proximity alert
                        addProximAlert(point);

                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);

                        InputStream is = null;

                        // add values to the database
                        String name = "" + editText1.getText();
                        String description = "" + editText2.getText();
                        String latitude = "" + Double.toString(point.latitude);
                        String longitude = "" + Double.toString(point.longitude);

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
                        nameValuePairs.add(new BasicNameValuePair("name", name));
                        nameValuePairs.add(new BasicNameValuePair("description", description));
                        nameValuePairs.add(new BasicNameValuePair("latitude", latitude));
                        nameValuePairs.add(new BasicNameValuePair("longitude", longitude));
                        nameValuePairs.add(new BasicNameValuePair("user","Someone"));

                        try {
                            HttpClient httpClient = new DefaultHttpClient();

                            HttpPost httpPost = new HttpPost("XXXXX/data2.php");
                            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                            HttpResponse response = httpClient.execute(httpPost);
                            HttpEntity entity = response.getEntity();
                            is = entity.getContent();
                            String msg = "Data has been sent successfully";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
                        } catch (ClientProtocolException e) {
                            Log.e("ClientProtocol", "Log_Tag");
                            e.printStackTrace();
                            String msg2 = "Log_Tag";
                            Toast.makeText(getApplicationContext(), msg2, Toast.LENGTH_LONG).show();
                        } catch (IOException e) {
                            Log.e("Log_Tag", "IOException");
                            e.printStackTrace();
                            String msg3 = "IOException";
                            Toast.makeText(getApplicationContext(), msg3, Toast.LENGTH_LONG).show();
                        }
                    }

                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

我的PHP文件如下:

<?php

function createNewProduct() {
        $response = array();
        $name=$_POST['name'];
        $description=$_POST['description'];
        $latitude=$_POST['latitude'];
        $longitude=$_POST['longitude'];
        $user=$_POST['user'];
        //$db = new DbConnect();

        $connection = mysqli_connect('localhost', 'XXXXXXXXX', 'XXXX','XXXX');

        $query = "INSERT INTO pois(name,description,lat,longit,created_by) VALUES('$name','$description','$latitude','$longitude','$user')";
        $result = mysqli_query($connection, $query) or die(mysql_error());
        if ($result) {
            $response["error"] = false;
            $response["message"] = "OK!";
        } else {
            $response["error"] = true;
            $response["message"] = "Failed!";
        }
       // echo json response
    echo json_encode($response);
}
createNewProduct();
?>

有什么解决办法吗?

更新:谢谢大家的帮助! 我发现了错误所在。这样说很可惜,但是我在我的 php 文件中发送了错误的密码,现在一切正常。

  • 写回答

2条回答

  • doudi4137 2015-07-06 11:58
    关注

    You might want to test the PHP, use a tool to send the data to the php (like cURL for instance), and check that your server behaves as expected first.

    Then, check that your android can correctly

    httpClient.execute(httpPost);
    

    on any server, and post the results.

    Likely, the Android disabled the HttpCliente API (if Android 22+)

    评论

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。