gpt4 book ai didi

java - Android PHP POST 登录

转载 作者:行者123 更新时间:2023-12-01 11:32:32 25 4
gpt4 key购买 nike

我尝试在其他帖子中寻找答案并避免重复发布;然而,许多人使用的是已弃用的方法。我正在尝试创建一个登录 Activity ,它将用户详细信息发布到 php 并接收 jsonEncoded 状态“已授权”或“用户详细信息不正确”响应;我已经测试了 PHP,它可以工作。当我单击登录按钮时,它将执行以下操作:

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnLogin:

String username = Username.getText().toString();
String password = Password.getText().toString();

User user = new User(username, password);

authenticate(user);

userLocalStore.setUserLoggedIn(true);
userLocalStore.storeUserData(user);

break;
}
}

它包含了User函数,这让我更容易传递信息。

String username, password;

public User (String username, String password) {
this.username = username;
this.password = password;
}

构造用户后,登录将调用身份验证函数:

private void authenticate(User user) {

ServerRequests serverRequests = new ServerRequests(this);
serverRequests.fetchUserDataAsyncTask(user, returnedUser);{

if (returnedUser == null){
showErrorMessage();
} else {
logUserIn();
}
};
}

用户通过身份验证后,它将登录用户并启动新 Activity :

private void logUserIn() {
userLocalStore.setUserLoggedIn(true);
startActivity(new Intent(this, Map.class));
}

我遇到的问题是,它似乎没有调用 php,而且我不确定哪里出了问题;我尝试过将其他人所做的事情拼凑在一起,但它不起作用。如果有任何帮助,我将不胜感激。

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class ServerRequests {

URL url;
HttpURLConnection conn;
ProgressDialog progressDialog;

public ServerRequests(Context context) {
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Authenticating...");
progressDialog.setMessage("Please wait...");
}

public void fetchUserDataAsyncTask(User user, String returnedUser) {
progressDialog.show();
new fetchUserDataAsyncTask(user, returnedUser).execute();
}

public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, String> {
User user;
String returnedUser;


public fetchUserDataAsyncTask(User user, String returnedUser) {
this.user = user;
this.returnedUser = returnedUser;
}

@Override
protected String doInBackground(Void... params) {

try {

url = new URL("www.mysite.com/test.php");

conn = (HttpURLConnection) url.openConnection();
String param = "username="+user.username+"&password="+user.password;
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "UTF-8");
conn.connect();

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(param);

//No code to receive response yet, want to get the POST working first.

}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {

} finally {
progressDialog.dismiss();
}

return returnedUser;
}
}
}

最佳答案

试试这个..

conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
String param = "username="+user.username+"&password="+user.password;
OutputStream out = conn.getOutputStream();
DataOutputStream dataOut = new DataOutputStream(out);
dataOut.writeBytes(param.trim());
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
StringBuilder builder = new StringBuilder();
while((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
System.out.println(builder.toString());

StringBuilder中,您将获得服务器返回的输出。

它将把你的参数提交到 php 页面..

关于java - Android PHP POST 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30287968/

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