2017-08-21 54 views
2

我需要更新Android中的mysql表我嘗試使用Post方法,但不起作用,請幫助我。 我已經創建了一個從安卓應用程序接收「id」的PHP頁面。理論上應用程序發送「id」拋出HttpUrlConnectiondownload()執行但不起作用,我不明白如何使用它。 我爲我的壞英語道歉。在Android中使用HttpURLConnection更新mySql表

這是我PHP

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){ 
//Getting values 
$id = $_POST['id']; 



//importing database connection script 
require_once('dbConnect.php'); 

//Creating sql query 
$sql = "UPDATE tbl_mp3 SET Download=Download+1 WHERE id = $id"; 

//Updating database table 
if(mysqli_query($con,$sql)){ 
echo 'DownloadUpdated Successfully'; 
}else{ 
echo 'Could Not Update Download Try Again'; 
} 

//closing connection 
mysqli_close($con); 
} 

,這是我MainActivity.java

public void countdownload() throws Exception { 

     URL url = new URL("myphp"); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setReadTimeout(10000); 
     urlConnection.setConnectTimeout(15000); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setDoInput(true); 
     urlConnection.setDoOutput(true); 


     String id=Constant.TAG_ID; 


     OutputStream os = urlConnection.getOutputStream(); 
     BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(os, "UTF-8")); 
     writer.write(id); 
     writer.flush(); 
     writer.close(); 
     os.close(); 

     urlConnection.connect(); 


    } 

    private void download() throws Exception { 
     File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + getString(R.string.download_desti)); 

     if(!root.exists()) { 
      root.mkdirs(); 
     } 

     File file = new File(root,Constant.arrayList_play.get(Constant.playPos).getMp3Name()+".mp3"); 

     if(!file.exists()) { 
      String url = Constant.arrayList_play.get(Constant.playPos).getMp3Url(); 
      DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
      request.setDescription(getResources().getString(R.string.downloading) + " - " + Constant.arrayList_play.get(Constant.playPos).getMp3Name()); 
      request.setTitle(Constant.arrayList_play.get(Constant.playPos).getMp3Name()); 
      // in order for this if to run, you must use the android 3.2 to compile your app 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
       request.allowScanningByMediaScanner(); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
      } 
      request.setDestinationInExternalPublicDir(getString(R.string.download_desti), Constant.arrayList_play.get(Constant.playPos).getMp3Name() + ".mp3"); 

      // get download service and enqueue file 
      DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
      manager.enqueue(request); 
      //add 1 to dowload string on DB 
      this.countdownload(); 

     } else { 
      Toast.makeText(MainActivity.this, getResources().getString(R.string.already_download), Toast.LENGTH_SHORT).show(); 
     } 
    } 

我不明白如何使用Httpconnection。 謝謝你的幫助。

+0

它掉下來了?添加你的logcat。 – Fori

+0

沒有不簡單的數據庫沒有更新 –

+0

所以,嘗試讀取連接inputstream:'urlConnection.getInputStream()'來獲得服務器響應。 – Fori

回答

0

如果你不明白「如何使用Http連接」...這將幫助你這麼多here。這是從應用程序向您的網頁發送/接收數據的完整示例。

+0

我以前見過,但使用HttpClient,並不適用於我的應用程序。 謝謝 –

+0

實際上它提供了更好的性能。但是,如你所願,最終它屬於你:) – HasH

+0

所以我也會試試這個,謝謝! –