2013-04-22 105 views
1

我試圖訪問數據庫中的個人播放器,我得到的答覆{「success」:0,「message」:「必填字段丟失」}但這是錯誤的,應該輸出一個單一的球員領域。從android數據庫中獲取數據使用php

我認爲問題是與PHP代碼,但我不確定。任何幫助非常感謝,謝謝。

PHP代碼 -

<?php 

require('db_connection.php'); 

// check for post data 
if (isset($_GET["playerid"])) { 
$playerid = $_GET['playerid']; 

// get a player from week1 table 
$result = mysql_query("SELECT *FROM week1 WHERE playerid = $playerid"); 

if (!empty($result)) { 
    // check for empty result 
    if (mysql_num_rows($result) > 0) { 

     $result = mysql_fetch_array($result); 

     $player = array(); 
     $player["playerid"] = $result["playerid"]; 
     $player["score"] = $result["score"]; 
     $player["lastholeplayed"] = $result["lastholeplayed"]; 
     $player["overall"] = $result["overall"]; 

     // success 
     $response["success"] = 1; 

     // user node 
     $response["player"] = array(); 

     array_push($response["player"], $player); 

     // echoing JSON response 
     echo json_encode($response); 
    } else { 
     // no player found 
     $response["success"] = 0; 
     $response["message"] = "No player found"; 

     // echo no users JSON 
     echo json_encode($response); 
    } 
} else { 
    // no player found 
    $response["success"] = 0; 
    $response["message"] = "No player found"; 

    // echo no users JSON 
    echo json_encode($response); 
} 
} else { 
// required field is missing 
$response["success"] = 0; 
$response["message"] = "Required field(s) is missing"; 

// echoing JSON response 
echo json_encode($response); 
} 
?> 

相關的Java類 -

public class InputScores extends Activity { 


// Progress Dialog 
    private ProgressDialog pDialog; 

    // Creating JSON Parser object 


    ArrayList<HashMap<String, String>> holesList; 

    // url to get all products list 
    private static String url_update_players = "http://192.168.2.4/realdeal/getplayer.php"; 

    // JSON Node names 


    // products JSONArray 
    JSONArray courseone = null; 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (pDialog != null) { 
      pDialog.cancel(); 
     } 
    } 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.updatescores); 

    Intent i = getIntent(); 
    i.getStringExtra("playerid"); 

    //String playerid = i.getStringExtra("playerid"); 
    //System.out.println(playerid); 
    //TextView myTextView = (TextView)findViewById(R.id.); 
    //myTextView.setText(playerid); 
    //String playerid = "999"; 
    new loadplayerdetails().execute(); 

} 
/** 
* Background Async Task to Load all holes by making HTTP Request 
* */ 
class loadplayerdetails extends AsyncTask<String, String, String> { 




    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(InputScores.this); 
     pDialog.setMessage("Loading holes. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    /** 
    * getting All holes from url 
    * */ 
    protected String doInBackground(String... params) { 

     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url_update_players); 

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
        2); 
      String playerid ="5"; 
      nameValuePairs.add(new BasicNameValuePair("playerid", playerid)); 



      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 

      String response = httpclient.execute(httppost, 
        responseHandler); 
      // you will get json string here 

      // check here your getting json string in logcat. 
      Log.d("response", response); 



     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all players 
     if (pDialog.isShowing()) { 
      pDialog.dismiss(); 
     } 


    } 

} 

}

+0

似乎一切都非常好...也許嘗試刪除從2: 名單<的NameValuePair> namevaluepairs中=新的ArrayList <的NameValuePair>(2); – 2013-04-22 23:45:38

+0

試過,但沒有工作不幸。 – dGray 2013-04-22 23:52:37

回答

1

您沒有使用 「獲取」 發佈您的數據。

,當你讓你的電話與該URL的一切可以使用$ _GET:

http://blahblah.com/somescript.php?playerid=1234 

在你的代碼中,您使用$ _GET得到它。當你張貼,像你一樣使用$ _ POST:

if (isset($_POST["playerid"])) { 
    $playerid = $_POST['playerid']; 
+0

謝謝,我改變了你的建議,但我現在得到了什麼似乎是我的迴應中的html樣式代碼。 – dGray 2013-04-23 00:20:48

+0

這聽起來像是原始問題已修復,但您可能需要調試一下您的PHP代碼。您可能想要創建一個快速發佈數據的網頁,以便您可以單獨測試您的php腳本。 – Dave 2013-04-23 00:47:36

+0

謝謝我已經試過這種方式,雖然我仍然不確定它爲什麼不工作.. – dGray 2013-04-23 01:10:47

相關問題