gpt4 book ai didi

java - Android 中的 HTML 请求

转载 作者:行者123 更新时间:2023-12-01 12:39:09 26 4
gpt4 key购买 nike

所以这是我的问题:我正在开发一个需要登录的移动应用程序。我正在 Android Studio/Java 中编程。在Java方面有很好的经验,但我从未做过网络。服务器上有一个处理登录的.asp脚本,我需要将登录数据发送到该脚本。我认为解决这个问题的最佳方法是 HTTP 请求,因为如果您在浏览器中输入脚本的 url,然后输入包含登录数据的查询字符串,您就已经得到了响应。

http://sampleurl.info/actions/checklogin.asp?userName=klingenhaeger&password=droid&device=android

返回一个包含配置文件 token 、时间戳和配置文件名称的 Json 字符串。例如:

{"profil_token":"qn2hJcRQixYjG7yyW956g1407921902","profil_name":"Marc Klingenhäger","timestamp":"1407921902"}

然后,此配置文件 token 会附加到用户请求的每个 URL,这样用户就可以获得所有网站的权限。我读到你可以使用 http GET 请求做同样的事情,但我和我的同事正在研究这个(这么简单的事情)对于我们的九个人来说并没有让我们的代码工作......我们尝试了很多 fragment ,这是我们最简单的尝试:在 Main Activity 中,单击导致登录的按钮时,将使用 Intent 调用 LoginActivity.class。

        Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);

输入用户数据后,用户点击登录按钮,方法attemptLogin();被调用。

public void attemptLogin() {
if (mAuthTask != null) {
return;
}

// Reset errors.
mEmailView.setError(null);
mPasswordView.setError(null);

// Store values at the time of the login attempt.
String email = mEmailView.getText().toString();
String password = mPasswordView.getText().toString();

boolean cancel = false;
View focusView = null;


// Check for a valid password, if the user entered one.
if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
mPasswordView.setError(getString(R.string.error_invalid_password));
focusView = mPasswordView;
cancel = true;
}

// Check for a valid email address.
if (TextUtils.isEmpty(email)) {
mEmailView.setError(getString(R.string.error_field_required));
focusView = mEmailView;
cancel = true;
} else if (!isEmailValid(email)) {
mEmailView.setError(getString(R.string.error_invalid_email));
focusView = mEmailView;
cancel = true;
}

if (cancel) {
// There was an error; don't attempt login and focus the first
// form field with an error.
focusView.requestFocus();
} else {
// Show a progress spinner, and kick off a background task to
// perform the user login attempt.
showProgress(true);
mAuthTask = new UserLoginTask(email, password);
mAuthTask.execute((Void) null);
}
}

因此,经过一些错误检测后,类 userLoginTask (AsyncTask 的子类)被初始化来处理网络内容,因为在主线程中初始化 http 请求似乎会导致异常。到目前为止,我们还没有设法在这里编写 HTTP - 请求代码..(这是主要问题)

公共(public)类 UserLoginTask 扩展 AsyncTask {

    private final String mEmail;
private final String mPassword;

UserLoginTask(String email, String password) {
mEmail = email;
mPassword = password;
}

@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.

try {
// Simulate network access.
Thread.sleep(2000);
} catch (InterruptedException e) {
return false;
}

for (String credential : DUMMY_CREDENTIALS) {
String[] pieces = credential.split(":");
if (pieces[0].equals(mEmail)) {
// Account exists, return true if the password matches.
return pieces[1].equals(mPassword);
}
}

// TODO: register the new account here.
return true;
}

@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);

if (success) {
finish();
} else {
mPasswordView.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
}

@Override
protected void onCancelled() {
mAuthTask = null;
showProgress(false);
}
}

所以我的问题基本上是,如何在 UserLoginTask 类中初始化 HTTP - 请求。有任何想法吗?提前致谢! :)

法尔科

最佳答案

最简单的方法是使用 URL 对象并打开到 HTTP 服务器的流。
可以通过此流读取服务器响应:

String url = "http://sampleurl.info/actions/checklogin.asp?userName=klingenhaeger&password=droid&device=android";
try {
URL u = new URL(url);
InputStream is = u.openStream(); // Opens streaming connection to url
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

StringBuffer result = new StringBuffer(); // Buffer to store saved data
String input = null;

while((input = reader.readLine()) != null) {
// Read data until the end of the stream
result.append(input);
}

// Do something with result here
} catch (IOException e) {
e.printStackTrace();
}

当您以字符串形式检索数据时,您可以解析 JSON 以获取 profile_token

关于java - Android 中的 HTML 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25281034/

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