我目前正在Android下创建一个应用程序,可以让我连接用户。我使用常规的android服务器请求。我调用存储在我的服务器上的一个php文件,看看用户输入的是当前在BDD中注册的,但它根本不起作用...
这是我的php文件:
<?php
$con = mysqli_connect("localhost", "user", "userpass", "Android");
$username = $_POST["username"];
$password = $_POST["password"]
$statement = mysqli_prepare($con, "SELECT * FROM Utilisateur 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, $userID, $name, $username, $mail, $password, $adresse, $ville, $zipCode);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user[name] = $name;
$user[username] = $username;
$user[mail] = $mail;
$user[password] = $password;
$user[adresse] = $adresse;
$user[ville] = $ville;
$user[zipCode] = $zipCode;
}
echo json_encode($user);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
这是我在Java中的请求函数(有一个用户:密码,因为您需要进行身份验证才能访问我服务器上的任何网页)
public static final int CONNECTION_TIMEOUT = 1000 * 15;
public static final String SERVER_ADDRESS = "http://user:userpassword@62.210.193.223/Android/";
public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallBack userCallBack;
public fetchUserDataAsyncTask(User user, GetUserCallBack callBack) {
this.user = user;
this.userCallBack = callBack;
}
@Override
protected User doInBackground(Void... params) {
User returnedUser;
try {
URL url = new URL(SERVER_ADDRESS + "FetchUserData.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
Uri.Builder builder = new Uri.Builder().appendQueryParameter("username", user.username)
.appendQueryParameter("password", user.password);
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = IOUtils.toString(in, "UTF-8");
JSONObject jResponse = new JSONObject(response);
if (jResponse.length() == 0) {
returnedUser = null;
} else {
String name = jResponse.getString("name");
String mail = jResponse.getString("mail");
String adresse = jResponse.getString("adresse");
String ville = jResponse.getString("ville");
int zipCode = jResponse.getInt("zipCode");
returnedUser = new User(name, user.name, user.password, mail, adresse, ville, zipCode);
}
return returnedUser;
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(User returnedUser) {
progressDialog.dismiss();
userCallBack.done(returnedUser);
super.onPostExecute(returnedUser);
}
}
我在调试模式下启动了我的应用程序,我抓住了这个例外:
java.io.FileNotFoundException: http://user:userpassword@62.210.193.223/Android/FetchUserData.php
我甚至尝试在网络浏览器中对网址进行C / C并执行该文件。当然它返回[]因为我在通话中没有包含POST。
我完全迷失了,因为android对我来说很新,而且我真的不知道在哪里寻找解决这个问题的方法......如果有人能帮助我解决这个问题,那将非常感激:)
干杯, ASTRUS
答案 0 :(得分:0)
您似乎正在使用BasicAuth进行HTTP安全性。提供用户名和密码不是为您的请求提供基本凭据的好方法。
您需要设置正确的授权标头:
URLConnection uc = url.openConnection();
String userpass = username + ":" + password;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty ("Authorization", basicAuth);
请参阅:Connecting to remote URL which requires authentication using Java