gpt4 book ai didi

php - 将 MySQL 连接到 Android

转载 作者:行者123 更新时间:2023-11-29 17:27:22 25 4
gpt4 key购买 nike

我尝试通过 php 将 Android 连接到 MySQL。我的代码没有错误,但我猜有问题。我尝试运行该应用程序,但仅显示对话框的标题。我认为结果可能是问题所在?

public class BackgroundWorker extends AsyncTask<String,Void,String> {

Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context = ctx;
}

@Override
protected String doInBackground(String[] voids) {
String type = "login";

String login_url = "http://192.168.254.109/ITSP/login.php";
if(type.equals("login")){
try {
String username = voids[1];
String password =voids[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();
}
}
return null;
}


@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}

@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}

通过从这里获取值:

public void OnLogin(View view){
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, password);
}

我刚刚使用了简单的 PHP 连接代码。 (conn.php)

当我按下登录按钮时,仅显示alertDialog = "Login Status"

最佳答案

我建议您使用Ion库而不是使用AsyncTask。它会减少代码行数,同时比 AsyncTask

更准确

https://github.com/koush/ion

这是该库的 github 链接。

您在代码中尝试执行的操作可以像这样实现

    Ion.with(getContext())
.load("POST","http://192.168.254.109/ITSP/login.php")
.setBodyParameter("username", username)
.setBodyParameter("password", password)
.asJsonObject() //change it to .asString() if u expect the response from the server in a string format
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// do stuff with the result or error
//if you change the return to .asString() u need to change the callback method to <String> instead of<JsonObject>
});

关于php - 将 MySQL 连接到 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50886209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com