场景:我正在尝试使用与 Moodle 实例关联的 MYSQL 数据库,以便拥有 Moodle 帐户的人可以登录应用程序。我按照教程编写了一个小应用程序,但是我不确定我可能犯的错误。
问题:当我运行应用程序时,我会看到登录表单;用户名、密码和一个按钮。点击它,但什么也没发生。
PHP代码:
<?php
require_once 'include/config.php';
//Values
$username_in = $_POST['username'];
$password_in = $_POST['password'];
//Values Test
//$username_in = "guest"
//$password_in = $_POST['password'];
$query = "SELECT * from moodle_user where username ='".$username_in."' OR password = '".$password_in."'";
//TEST: $query = "SELECT * from moodle_user";
$query_exec = mysqli_query($con, $query) or die(mysqli_error($con));
//if and else
$rows = mysqli_num_rows($query_exec);
if($rows == 0) {
echo "No user found";
mysqli_fetch_row ($con,$query);
} else {
echo "Found user: " . $username_in;
}
//Make your query and store it in $result
while ($rows = mysqli_fetch_assoc($query_exec))
{
echo $rows['username'];
}
?>
爪哇:
package com.profiledt.helloworld;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
Button login;
EditText username, password;
TextView status;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePair;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setup();
}
private void setup() {
// TODO Auto-generated method stub
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
status = (TextView) findViewById(R.id.greetingtext);
login.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.login:
login();
break;
}
}
private void login() {
// TODO Auto-generated method stub
try {
httpclient= new DefaultHttpClient();
httppost= new HttpPost("http://profiledt.co.uk/android/index.php");
// Adding Data:
nameValuePair = new ArrayList<NameValuePair>(1);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePair.add(new BasicNameValuePair("username",username.getText().toString().trim()));
nameValuePair.add(new BasicNameValuePair("password",password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
status.setText(""+response);
if(response.equalsIgnoreCase("No user found")) {
startActivity(new Intent(MainActivity.this, UserPage.class));
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
谢谢