2016-03-20 25 views
0

Kullanıcıların Android uygulamamda oturum açmasına izin vermeye çalışıyorum. Kullanıcı adı ve şifreyi sunucuya gönderdiğimde, kullanıcının detayları ile bir JSON dizisi döndürmelidir. Diziyi döndürüyor ama aynı zamanda HTML kodunun bir sürü. Bunun neden olduğunu ve nasıl düzeltileceğini bilen var mı? TeşekkürlerAndroid php sunucusundan garip bir sonuç alıyor

Sunucudan yanıt İşte. İhtiyacım olan JSON dizisini takip eden 5 kez tekrarlanan aynı HTML bloğu.

<br /> 
    <font size='1'><table class='xdebug-error xe-notice' 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> Notice: Use of undefined constant student_id - assumed 'student_id' in C:\wamp64\www\fetchuserdata.php on line <i>17</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.0005</td><td bgcolor='#eeeeec' align='right'>241296</td><td bgcolor='#eeeeec'>{main}()</td><td title='C:\wamp64\www\fetchuserdata.php' bgcolor='#eeeeec'>...\fetchuserdata.php<b>:</b>0</td></tr> 
    </table></font> 
<br /> 
    <font size='1'><table class='xdebug-error xe-notice' 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> Notice: Use of undefined constant student_id - assumed 'student_id' in C:\wamp64\www\fetchuserdata.php on line <i>17</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.0005</td><td bgcolor='#eeeeec' align='right'>241296</td><td bgcolor='#eeeeec'>{main}()</td><td title='C:\wamp64\www\fetchuserdata.php' bgcolor='#eeeeec'>...\fetchuserdata.php<b>:</b>0</td></tr> 
    </table></font> 
<br /> 
    <font size='1'><table class='xdebug-error xe-notice' 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> Notice: Use of undefined constant student_id - assumed 'student_id' in C:\wamp64\www\fetchuserdata.php on line <i>17</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.0005</td><td bgcolor='#eeeeec' align='right'>241296</td><td bgcolor='#eeeeec'>{main}()</td><td title='C:\wamp64\www\fetchuserdata.php' bgcolor='#eeeeec'>...\fetchuserdata.php<b>:</b>0</td></tr> 
    </table></font> 
<br /> 
    <font size='1'><table class='xdebug-error xe-notice' 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> Notice: Use of undefined constant student_id - assumed 'student_id' in C:\wamp64\www\fetchuserdata.php on line <i>17</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.0005</td><td bgcolor='#eeeeec' align='right'>241296</td><td bgcolor='#eeeeec'>{main}()</td><td title='C:\wamp64\www\fetchuserdata.php' bgcolor='#eeeeec'>...\fetchuserdata.php<b>:</b>0</td></tr> 
    </table></font> 
<br /> 
    <font size='1'><table class='xdebug-error xe-notice' 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> Notice: Use of undefined constant student_id - assumed 'student_id' in C:\wamp64\www\fetchuserdata.php on line <i>17</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.0005</td><td bgcolor='#eeeeec' align='right'>241296</td><td bgcolor='#eeeeec'>{main}()</td><td title='C:\wamp64\www\fetchuserdata.php' bgcolor='#eeeeec'>...\fetchuserdata.php<b>:</b>0</td></tr> 
    </table></font> 
{"student_id":10381109,"fname":"Jack":"lname":"Finan","username":"123","password":"123"} 

Bu benim Java kodu:

public class MainActivity extends AppCompatActivity implements View.OnClickListener 
{ 

TextView responseView; 
EditText etUsername; 
EditText etPassword; 
ProgressBar progressBar; 
Button bLogin; 
static final String SERVER_ADDRESS = "http://192.168.1.11:8080/"; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    responseView = (TextView) findViewById(R.id.responseView); 
    etUsername = (EditText) findViewById(R.id.etUsername); 
    etPassword = (EditText) findViewById(R.id.etPassword); 
    progressBar = (ProgressBar) findViewById(R.id.progressBar); 
    bLogin = (Button) findViewById(R.id.bLogin); 
    bLogin.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) 
{ 
    switch(v.getId()){ 
     case R.id.bLogin: 
      String username = etUsername.getText().toString(); 
      String password = etPassword.getText().toString(); 

      User user = new User(username, password); 

      getUser(user); 
      break; 
    } 
} 

public void getUser(User user) 
{ 
    new GetJSONData(user).execute(); 
} 

class GetJSONData extends AsyncTask<Void, Void, String> // class to fetch javascript object data 
{ 
    User user; // variable 

    GetJSONData(User user) // constructor 
    { 
     this.user = user; 
    } 

    protected void onPreExecute() // methods below 
    { 
     progressBar.setVisibility(View.VISIBLE); 
     responseView.setText(""); 
    } 

    protected String doInBackground(Void... urls) 
    { 

     HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     try { 
      URL url = new URL(SERVER_ADDRESS + "fetchuserdata.php"); // the server we hit and the course code we want entered 
      connection = (HttpURLConnection) url.openConnection(); // open connection 
      connection.setRequestMethod("POST"); 

      Uri.Builder builder = new Uri.Builder().appendQueryParameter("username", user.username).appendQueryParameter("password",user.password); 
      String query = builder.build().getEncodedQuery(); 
      OutputStream os = connection.getOutputStream(); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8")); 
      writer.write(query); 
      writer.flush(); 
      writer.close(); 
      os.close(); 

      connection.connect(); // connect 

      InputStream stream = connection.getInputStream(); // create input stream of data 
      reader = new BufferedReader(new InputStreamReader(stream)); // create buffered reader to help read input stream of data 

      StringBuilder buffer = new StringBuilder(); 
      String line; 

      while ((line = reader.readLine()) != null) { 
       buffer.append(line).append("\n"); 
      } 

      return buffer.toString(); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      if(connection != null) 
      { 
       connection.disconnect(); 
      } 
      try 
      { 
       if(reader != null) 
       { 
        reader.close(); 
       } 
      } 
      catch(IOException e) 
      { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    protected void onPostExecute(String result) 
    { 
     if(result == null) { 
      result = "THERE WAS AN ERROR"; 
     } 
     progressBar.setVisibility(View.GONE); 
     Log.i("INFO", result); 
     responseView.setText(result); 
    } 
} 
} 

Ve PHP:

<?php 
$con=mysqli_connect('localhost','root','164f9ogC!','user'); 

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

$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?"); 
mysqli_stmt_bind_param($statement, "ss", $username, $password); 
mysqli_stmt_execute($statement); 

mysqli_stmt_store_result($statement); 
mysqli_stmt_bind_result($statement, $student_id, $fname, $lname, $username, $password); 

$user = array(); 

while(mysqli_stmt_fetch($statement)){ 
    $user[student_id] = $student_id; 
    $user[fname] = $fname; 
    $user[lname] = $lname; 
    $user[username] = $username; 
    $user[password] = $password; 
} 

echo json_encode($user); 

mysqli_stmt_close($statement); 

mysqli_close($con); 
?> 
+0

Bu html sonucu bir uyarıdır. Oku onu. Bu uyarıyı yapan parçanın nerede göründüğünü göreceksiniz. – Raizal

cevap

3

PHP hata var çünkü HTML alıyorsanız. HTML bloklarını okursanız, hatanın while döngüsünde olduğunu görürsünüz. aksi takdirde bir sabiti ulaşmaya çalışıyorsunuz, PHP dizi indeksi için tırnak olması gerekir

while(mysqli_stmt_fetch($statement)){ 
    $user['student_id'] = $student_id; 
    $user['fname'] = $fname; 
    $user['lname'] = $lname; 
    $user['username'] = $username; 
    $user['password'] = $password; 
} 

: olarak değiştirin.

+0

Yup, öyleydi. Çok teşekkürler. –

1

Her şeyden önce mysqli_ * kullanmayı bırakın. Bu kullanımdan kaldırılmış ve çok güvenlidir. PDO'ya son derece güvenli olarak taşıyın.

Ayrıca, $ kullanıcı için Tırnakları eklemeyi de unutmuşsunuzdur.

while(mysqli_stmt_fetch($statement)){ 
    $user['student_id'] = $student_id; 
    $user['fname'] = $fname; 
    $user['lname'] = $lname; 
    $user['username'] = $username; 
    $user['password'] = $password; 
} 

Ben JavaScript olduğunu ve tek Yankı etiketi $ Kullanıcı etiketi için olduğu gibi bilgileri alınamıyor genellikle PHP gibi HTML çıkışı ne olduğunu teyit edebiliriz.

Javascript'i çok iyi bilmiyorum, tüm söyleyebileceğim tek şey Javascript'inizin keskin bir gözü.

İlgili konular