2015-07-06 25 views
1

我想從我的android應用程序保存一些數據到服務器上的mysql數據庫。問題是,一切似乎都完美地工作,我收到消息,我的數據已成功發送,但我的數據庫沒有收到任何東西。 我有以下文件:Android Studio - 發送數據到mySql databse

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文件中發送了錯誤的密碼。現在一切正常。

+0

你從服務器得到什麼迴應? –

+0

首先從InputStream'讀取響應並查看它告訴的內容。 – Raja

回答

0

您可能想要測試PHP,使用一種工具將數據發送到php(例如cURL),並檢查您的服務器的行爲是否與預期的一樣。

然後,檢查您的Android能夠正確

httpClient.execute(httpPost); 

任何服務器上,並張貼結果。

可能的是,Android的禁用HttpCliente API(Android是否22+)

+0

它不是打字錯誤。這是正確的。該對象在這一行中聲明:'HttpPost httpPost = new HttpPost(「XXXXX/data2.php」);' –

+0

我很抱歉,我錯誤地誤讀了它。對您的要求有什麼迴應?是「OK​​!」還是「Failed!」,並且在您的服務器上,接收到的請求是什麼?從wamp - > apache - >訪問日誌中,您可以看到收到了什麼請求。張貼在你的問題上 – Bonatti

0

我會嘗試在PHP腳本回聲應答,看看數據實際上涉及到服務器。然後你可以看到android是否完成它的任務,你可以將問題隔離到android或php代碼。 此外,我會建議使用Retrofit或抽象這樣的GET或POST請求。