我是新的Android Studio編程,我試圖將我的應用程序連接到MySQL服務器上的MySQL數據庫,echo "login success !!!!! Welcome $user";
正常工作正常,但我希望得到一個PHP變量,如$user
也回到我的android studio開發,以便我可以使用它在應用程序中創建會話,但我無法獲取變量 我得到的回聲,我用它在其他地方顯示登錄成功。需要幫助謝謝如何獲得一個PHP變量回到android應用程序?
<?php
require "pconn.php";
$username = $_POST["username"];
$password = $_POST["password"];
$username = stripslashes($username);
$password = stripslashes($password);
$hash = md5($password);
$qry = "SELECT * FROM UserLogin WHERE Email='".$username."' and BINARY Password ='".$hash."'";
$result = mysqli_query($conn ,$qry);
if(mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$user = $row['Username'] ;
echo "login success !!!!! Welcome $user";
echo $user;
}
else {
echo "login not success";
}
?>
和我的Java類:
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://xaas.com/login.php";
if(type.equals("login")) {
try {
String username = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
'如何獲得一個PHP變量回到android studio?'錯誤的問題。我想你想要在Android設備上運行的客戶端應用程序中接收該變量的值。當您的應用在您的設備或其他人上運行時,它與Android Studio無關。 – greenapps
@greenapps我認爲你的意思是它有**絕對沒有**與Android Studio做。 –