2015-08-29 71 views
0

我是新來的Android編程,我試圖從本地主機數據庫做一個簡單的註冊登錄系統。註冊工作正常,但是當我試圖從登錄中獲得響應時,我得到了一些奇怪的xml \ html類文本,而不僅僅是數據。Java android httpURLConnection很奇怪

這是Java代碼:

@Override 
    protected User doInBackground(Void... params) 
    { 
     HashMap<String, Object> dataToSend = new HashMap<>(); 
     dataToSend.put("username", user.getUsername()); 
     dataToSend.put("password", user.getPassword()); 

     URL server; 
     HttpURLConnection httpRequestParams; 
     User returnedUser = null; 
     String response = ""; 
     try { 
      server = new URL(SERVER_ADDRESS + "FetchUserData.php"); 
      httpRequestParams = (HttpURLConnection)server.openConnection(); 

      httpRequestParams.setReadTimeout(10000); 
      httpRequestParams.setConnectTimeout(15000); 
      httpRequestParams.setRequestMethod("POST"); 
      httpRequestParams.setDoInput(true); 
      httpRequestParams.setDoOutput(true); 
      httpRequestParams.connect(); 

      OutputStream os = httpRequestParams.getOutputStream(); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
      System.out.println(dataToSend); 
      System.out.println(getPostDataString(dataToSend)); 
      writer.write(getPostDataString(dataToSend)); 
      writer.flush(); 
      writer.close(); 
      os.close(); 

      int responseCode = httpRequestParams.getResponseCode(); 
      System.out.println(responseCode); 

      if (responseCode == HttpURLConnection.HTTP_OK) 
      { 
       String line; 
       BufferedReader br = new BufferedReader(new InputStreamReader(httpRequestParams.getInputStream())); 
       while ((line = br.readLine()) != null) 
       { 
        response += line; 
        System.out.println(response); 
       } 
       br.close(); 
      } else 
      { 
       response = ""; 

       throw new Exception(responseCode + ""); 
      } 

      System.out.println(response); 
      JSONObject jObject = new JSONObject(response.toString()); 

      if(jObject.length() == 0) 
      { 
       returnedUser = null; 
      }else 
      { 
       String name = jObject.getString("name"); 
       int age = jObject.getInt("age"); 

       returnedUser = new User(name, user.getUsername(), user.getPassword(), age); 
      } 

     } catch (MalformedURLException e1) { 
      e1.printStackTrace(); 
     } catch (UnsupportedEncodingException e1) { 
      e1.printStackTrace(); 
     } catch (ProtocolException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
     } 

     return returnedUser; 
    } 

這是php文件:

<?php 
$con= new mysqli("localhost", "root", "", "users"); 

if ($con->connect_error) 
{ 
    die("Connection failed: " . $con->connect_error); 
} 

$username = $_POST["username"]; 
$password = $_POST["password"]; 

echo "HEY------------" . $username; 

$query = "SELECT * FROM users WHERE username='?' AND password='?'"; 
$statement = $con->prepare($query); 
$statement->bind_param("ss", $username, $password); 

if ($statement->execute() === TRUE) 
{ 
    echo "User selected successfully."; 
} else 
{ 
    echo "Error: " . $query . "<br>" . $con->error; 
} 

mysqli_stmt_store_result($statement); 
mysqli_stmt_bind_result($statement, $name, $username, $password, $age); 

mysqli_stmt_fetch($statement); 
$userDetails = array($name, $username, $password, $age); 
mysqli_stmt_close($statement); 

echo json_encode($userDetails); 

$con->close(); 
?> 

這是我得到的迴應:

<br /><font size='1'><table class='xdebug-error xe-notice xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Notice: Undefined index: username in D:\Programs\WampServer\www\FetchUserData.php on line <i>9</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}()</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-notice xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Notice: Undefined index: password in D:\Programs\WampServer\www\FetchUserData.php on line <i>10</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}()</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr></table></font>HEY------------<br /><font size='1'><table class='xdebug-error xe-warning xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Warning: mysqli_stmt::bind_param() [&lt;a href='mysqli-stmt.bind-param'&gt;mysqli-stmt.bind-param&lt;/a&gt;]: Number of variables doesn't match number of parameters in prepared statement in D:\Programs\WampServer\www\FetchUserData.php on line <i>16</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}()</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr><tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0111</td><td bgcolor='#eeeeec' align='right'>703168</td><td bgcolor='#eeeeec'>mysqli_stmt->bind_param()</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>16</td></tr></table></font>User selected successfully.[null,null,null,null] 

這是我得到的當我使用www文件夾中的php文件時:

User selected successfully.[user,username,pass,12] 

我不知道什麼是錯的......我不知道爲什麼反應是這樣的..

+0

什麼是你想要的預期的響應? – Tauqir

回答

0

錯誤告訴你的是,PHP並沒有得到在POST用戶名/密碼。在設置參數之前,請刪除httpRequestParams.connect(); - 連接太快。無論如何,getResponseCode()都會成功。

+0

做了它,但它所做的只是縮短了響應 –

+0

出於某種原因mysqli_stmt_fetch($語句)之後;用戶名和密碼變爲空。如果我評論該行,它不會獲取,但這是結果:用戶選擇成功。[null,username,pass,null] –

0

請刪除這些行

$userDetails = array($name, $username, $password, $age); 
echo json_encode($userDetails); 
+0

不幸的是沒有幫助..現在它只是不會回聲.. –

+0

你能告訴我你想要什麼迴應? – Tauqir

+0

這是我想要得到的,如果一切正常: 用戶選擇成功。[名稱,用戶名,密碼,年齡] –