2012-03-30 84 views
0

我的HttpPost代碼發佈到我的數據庫。但它會在其中發佈空白值。我對Android的整個部分都是新手,所以我確信它是一個非常愚蠢的錯誤,但希望有人能幫助我。HttpPost未正確發佈

的Android代碼:

String name = "test"; 
        String _score = String.valueOf(score); 
        String _time = String.valueOf(seconds); 

        try { 
         final HttpClient client = new DefaultHttpClient(); 
         final HttpPost post = new HttpPost(
           "http://www.laytproducts.com/plantzsubmithighscore.php"); 

         final List pair = new ArrayList(3); 
         pair.add(new BasicNameValuePair("name", name)); 
         pair.add(new BasicNameValuePair("score", _score)); 
         pair.add(new BasicNameValuePair("time", _time)); 

         post.setEntity(new UrlEncodedFormEntity(pair)); 

         HttpResponse httpResponse = client.execute(post); 
         HttpEntity entity = httpResponse.getEntity(); 
         InputStream is = entity.getContent(); 

         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
         StringBuilder sb = new StringBuilder(); 
         String line = null; 
         while((line=reader.readLine())!=null){ 
          sb.append(line+"\n"); 
         } 
         is.close(); 
         String result = sb.toString(); 
         Log.i("Plantz","FillBucket Highscore result:\n"+result); 
        } catch (Exception e) { 
         Log.e("Plantz", "Error in http connection: "+e.toString()); 
        } 

PHP的:

<?php 
    $con = mysql_connect("localhost","MY_USER","MY_PASS"); 
    $name = $_GET["name"]; 
    $score = $_GET["score"]; 
    $time = $_GET["time"]; 
    if(!$con){ 
    echo("COULDN'T CONNECT! " . mysql_error()); 
    die("COULDN'T CONNECT! " . mysql_error()); 
    } 
    mysql_select_db("laytprod_plantz",$con); 

    mysql_query("INSERT INTO bucket_highscore (Name, Score, Time) VALUES ('$name','$score','$time')"); 

    mysql_close(); 
?> 

回答

0

在你的Android的代碼,你要發送的查詢作爲通過HTTP POST郵件正文的一部分。在PHP代碼中,您試圖從$ _GET中讀取值。爲了讀取通過HTTP POST獲得的值,PHP代碼應該使用$ _POST或更好的$ _REQUEST。

+0

嗯。我想我的PHP知識甚至比我想象的要弱。它不與$ _REQUEST或$ _POST一起工作。 – Brandon 2012-03-30 17:51:17