Android 如何将editbox中的文本放入网站登录字段
嘿,伙计们,我想做的是有两个编辑框供用户登录网站。我不想使用webview。我的问题是如何从editbox获取输入值,并将其作为登录信息(即用户名和密码)输入,如果成功或失败,则返回结果Android 如何将editbox中的文本放入网站登录字段,android,webview,editbox,Android,Webview,Editbox,嘿,伙计们,我想做的是有两个编辑框供用户登录网站。我不想使用webview。我的问题是如何从editbox获取输入值,并将其作为登录信息(即用户名和密码)输入,如果成功或失败,则返回结果 再次感谢 事实上,我刚刚编写了一个应用程序,可以做到这一点。我的第一个问题是您在web端使用什么技术?对我来说是asp.NET。您可以使用GET或POST方法发送用户凭据并获取结果。我建议使用POST,因为信息是敏感的,POST更适合此类信息。这是代码 ASPX代码: string User = Request
再次感谢 事实上,我刚刚编写了一个应用程序,可以做到这一点。我的第一个问题是您在web端使用什么技术?对我来说是asp.NET。您可以使用GET或POST方法发送用户凭据并获取结果。我建议使用POST,因为信息是敏感的,POST更适合此类信息。这是代码 ASPX代码:
string User = Request.Form["u"];
string Pass = Request.Form["p"];
Response.Write(ValidateUser(u, p));
//I won't show you the validation code for security reasons. But I think you get the idea.
ANDROID代码:
public class LoginActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//Gets your login button and sets onClickListener
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
//Get your TextBox from the layout
EditText etUserName = (EditText)findViewById(R.id.etUser);
EditText etPassword = (EditText)findViewById(R.id.etPass);
//Client to make the request to your web page
DefaultHttpClient myClient = new DefaultHttpClient();
//This is where you put the information you're sending.
HttpPost postUserCredentials = new HttpPost(getString(R.string.LoginAddress));
HttpEntity postParameters = new StringEntity("u=" + etUserName.getText() + "&p=" + etPassword.getText());
postUserCredentials.setHeader("Content-type", "application/x-www-form-urlencoded");
postUserCredentials.setEntity(postParameters);
HttpResponse postResponse = myClient.execute(postUserCredentials);
HttpEntity postResponseEntity = postResponse.getEntity();
String result = EntityUtils.toString(postResponseEntity);
if (result.equals("1")) {
Toast.makeText(this, "Login Succeeded", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}
login.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tvUser" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Username:" />
<EditText android:id="@+id/etUser" android:layout_width="match_parent" android:layout_height="wrap_content" />
<TextView android:id="@+id/tvPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Password:" />
<EditText android:id="@+id/etPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" />
<Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" />
</LinearLayout>
事实上,我刚刚编写了一个应用程序,实现了这一点。我的第一个问题是您在web端使用什么技术?对我来说是asp.NET。您可以使用GET或POST方法发送用户凭据并获取结果。我建议使用POST,因为信息是敏感的,POST更适合此类信息。这是代码 ASPX代码:
string User = Request.Form["u"];
string Pass = Request.Form["p"];
Response.Write(ValidateUser(u, p));
//I won't show you the validation code for security reasons. But I think you get the idea.
ANDROID代码:
public class LoginActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//Gets your login button and sets onClickListener
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
//Get your TextBox from the layout
EditText etUserName = (EditText)findViewById(R.id.etUser);
EditText etPassword = (EditText)findViewById(R.id.etPass);
//Client to make the request to your web page
DefaultHttpClient myClient = new DefaultHttpClient();
//This is where you put the information you're sending.
HttpPost postUserCredentials = new HttpPost(getString(R.string.LoginAddress));
HttpEntity postParameters = new StringEntity("u=" + etUserName.getText() + "&p=" + etPassword.getText());
postUserCredentials.setHeader("Content-type", "application/x-www-form-urlencoded");
postUserCredentials.setEntity(postParameters);
HttpResponse postResponse = myClient.execute(postUserCredentials);
HttpEntity postResponseEntity = postResponse.getEntity();
String result = EntityUtils.toString(postResponseEntity);
if (result.equals("1")) {
Toast.makeText(this, "Login Succeeded", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}
login.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/tvUser" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Username:" />
<EditText android:id="@+id/etUser" android:layout_width="match_parent" android:layout_height="wrap_content" />
<TextView android:id="@+id/tvPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Password:" />
<EditText android:id="@+id/etPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" />
<Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" />
</LinearLayout>
太好了,我很高兴能帮上忙。如果此答案是您正在寻找的,请确保单击绿色复选标记“接受”。这将鼓励其他人回答你未来的问题。太好了,我很高兴能提供帮助。如果此答案是您正在寻找的,请确保单击绿色复选标记“接受”。这将鼓励其他人回答你未来的问题。