我正在尝试使用php连接一个带有e登录和注册的应用程序这是android连接部分的代码,url使用ftp连接mysql数据库:
public class BackgroundTask extends AsyncTask<String,Void,String> {
SharedPreferences preferences;
SharedPreferences.Editor editor;
Context context;
BackgroundTask(Context ctx){
this.context = ctx;
}
@Override
protected String doInBackground(String... params) {
preferences = context.getSharedPreferences("MYPREFS", Context.MODE_PRIVATE);
editor = preferences.edit();
editor.putString("flag","0");
editor.commit();
String urlRegistration = "ftp://remoteaddress/site/wwwroot/classes/LoginAndRegister-register.php";
String urlLogin = "ftp://remoteaddress/site/wwwroot/classes/LoginAndRegister-login.php";
String task = params[0];
if(task.equals("register")){
String regName = params[1];
String regEmail = params[2];
String regPassword = params[3];
try {
URL url = new URL(urlRegistration);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String myData = URLEncoder.encode("identifier_name","UTF-8")+"="+URLEncoder.encode(regName,"UTF-8")+"&"
+URLEncoder.encode("identifier_email","UTF-8")+"="+URLEncoder.encode(regEmail,"UTF-8")+"&"
+URLEncoder.encode("identifier_password","UTF-8")+"="+URLEncoder.encode(regPassword,"UTF-8");
bufferedWriter.write(myData);
bufferedWriter.flush();
bufferedWriter.close();
InputStream inputStream = httpURLConnection.getInputStream();
inputStream.close();
editor.putString("flag","register");
editor.commit();
return "Successfully Registered " + regName;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if(task.equals("login")){
String loginEmail = params[1];
String loginPassword = params[2];
try {
URL url = new URL(urlLogin);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
//send the email and password to the database
OutputStream outputStream = httpURLConnection.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String myData = URLEncoder.encode("identifier_loginEmail","UTF-8")+"="+URLEncoder.encode(loginEmail,"UTF-8")+"&"
+URLEncoder.encode("identifier_loginPassword","UTF-8")+"="+URLEncoder.encode(loginPassword,"UTF-8");
bufferedWriter.write(myData);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//get response from the database
InputStream inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String dataResponse = "";
String inputLine = "";
while((inputLine = bufferedReader.readLine()) != null){
dataResponse += inputLine;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println(dataResponse);
editor.putString("flag","login");
editor.commit();
return dataResponse;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
//This method willbe called when doInBackground completes... and it will return the completion string which
//will display this toast.
@Override
protected void onPostExecute(String s) {
String flag = preferences.getString("flag","0");
if(flag.equals("register")) {
Toast.makeText(context,s,Toast.LENGTH_LONG).show();
}
if(flag.equals("login")){
String test = "false";
String name = "";
String email = "";
String[] serverResponse = s.split("[,]");
test = serverResponse[0];
name = serverResponse[1];
email = serverResponse[2];
if(test.equals("true")){
editor.putString("name",name);
editor.commit();
editor.putString("email",email);
editor.commit();
Intent intent = new Intent(context,LogginIn.class);
context.startActivity(intent);
}else{
display("Login Failed...", "That email and password do not match our records :(.");
}
}else{
display("Login Failed...","Something weird happened :(.");
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
public void display(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
以下是php连接:
<?
$hostname = "us-cdbr-azure-southcentral-f.cloudapp.net";
$username = "XXXXX";
$dbname = "db";
$password = "word";
//Create connection
$conn = mysql_connect($hostname, $username, $password);
$selectdb=mysql_select_db($dbname);
//Check connection
if(!$conn){
//die("Connectioned failed!" . mysql_connect_error());
}
else{
//echo "Connect success!";
}
?>
并且login.php密码是加密的,所以我使用sha1()“x”来尝试获取密码:
<?php
require "LoginAndRegister-connection.php";
$user_email = $_POST["identifier_loginEmail"];
$user_pass = $_POST["identifier_loginPassword"];
$mysql_query = "SELECT * FROM usuarios WHERE email = '$user_email'
AND senha = '$user_pass'";
$result = mysql_query($conn,$mysql_query);
//check the result
if(mysql_num_rows($result)>0){
$row = mysql_fetch_assoc($result);
$name = $row["name"];
$email = $row["email"];
//Here is the specially formatted string response.. ie: "ServerResponse".
//It is of the form: "boolean,email,name"
echo "true,".$email.",".$name;
}
else{
echo "Login was not successful... :(";
}
?>
当我运行代码时,当我尝试登录app:java.lang.classcastexception:sun.net时,会出现此错误。www.protocol.ftp.ftpurlconnection 无法转换为java.net.httpurlconnection
有人能告诉我哪里犯的错吗?
暂无答案!
目前还没有任何答案,快来回答吧!