1

皆さん、私がやろうとしているのは、ユーザーが Web サイトにサインインするための 2 つの編集ボックスを用意することです。WebView を使用したくありません。私の質問は、編集ボックスから入力された値を取得し、それをサインイン情報 (つまり、ユーザー名とパスワード) として入力し、成功したかどうか結果を返すにはどうすればよいですか?

再度、感謝します!

4

1 に答える 1

1

私は実際に、これとまったく同じことを行うアプリを作成しました。私の最初の質問は、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.

アンドロイドコード:

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>
于 2011-06-22T23:40:47.767 に答える