2012-03-27 16 views
0

我正在使用以下代碼段在POST數據的Android應用程序中工作。如何在php腳本中獲取POSTED數據?

public void postData() 
     {  
      try 
      { 
       // Create a new HttpClient and Post Header 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/check/justprint.php"); 
      // Add your data 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("id", "jaydeepsinh jadeja")); 
       nameValuePairs.add(new BasicNameValuePair("password", "00000000000000")); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       // Execute HTTP Post Request 
       HttpResponse response = httpclient.execute(httppost);   
       String text = EntityUtils.toString(response.getEntity());   
        Log.i("","response = "+text); 
      } 
      catch (ClientProtocolException e) 
      { 
       Log.e("Error:",e+""); 
       e.printStackTrace(); 
      } 
      catch (IOException e) 
      { 
       Log.e("Error:",e+""); 
       e.printStackTrace(); 
      } 
     } 

我的主要要求是在php腳本中獲取這個值並在網頁上顯示。 有沒有辦法做同樣的事情?

回答

0

我認爲thatyou可以讓他們這樣

$id= $_POST['id']; 
$password= $_POST['password']; 

,然後就可以顯示出來。如果這不起作用,嘗試

$filename = __DIR__.DIRECTORY_SEPARATOR."logging.txt"; 

if(isset($_POST)){ 
    foreach ($_POST as $key => $value){ 
     file_put_contents($filename, "key[$key] - value[$value] post \n", FILE_APPEND); 
    } 
} 

,看看是否在該腳本的目錄中創建一個名爲logging.txt文件