0
ハードコーディングするとログインできますが、一般的な方法でログインしたいのですが、ログイン画面に資格情報が入力されていればログインする必要があります。私のURLは、ユーザー名とパスワードで構成さログインの資格情報を一般的な方法で作成したい場合
このよう
public class LoginScreen extends Activity {
private static final String TAG = "Login Screen";
public static final int CONNECTION_TIMEOUT=100000;
public static final int READ_TIMEOUT=15000;
private EditText editTextUsername;
private EditText editTextPassword;
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
editTextUsername = (EditText)findViewById(R.id.emaiId);
editTextPassword = (EditText)findViewById(R.id.password);
}
public void buttonLogin (View arg0)
{
username = editTextUsername.getText().toString();
password = editTextPassword.getText().toString();
new TaskLogin().execute(username,password);
}
public class TaskLogin extends AsyncTask<String,String,String>
{
ProgressDialog pdLoading = new ProgressDialog(LoginScreen.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pdLoading.setMessage("\t Loading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
StringBuilder result = new StringBuilder("unsuccessful");
try{
url = new URL(URLConstants.getLoginURL());
conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/json");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
writer.write("{\"email\":\"[email protected]\",\"password\":\"12345\"}");
writer.flush();
writer.close();
int response_code = conn.getResponseCode();
System.out.print("Result"+ response_code);
BufferedReader reader = null;
if(response_code==HttpURLConnection.HTTP_OK)
{
InputStream input = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(input));
result = new StringBuilder();
String line;
while ((line= reader.readLine())!=null)
{
result.append(line);
}
System.out.println("result "+result);
}
reader.close();
os.close();
conn.disconnect();
} catch (Exception e1)
{
e1.printStackTrace();
return "Exception";
}
return result.toString();
}
@Override
protected void onPostExecute(String result) {
pdLoading.dismiss();
if(result.equalsIgnoreCase("true"))
{
Intent intent = new Intent(LoginScreen.this,Main2Activity.class);
startActivity(intent);
LoginScreen.this.finish();
}else if (result.equalsIgnoreCase("false"))
{
Toast.makeText(LoginScreen.this, "Invalid email or password", Toast.LENGTH_LONG).show();
} else if(result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful"))
{
Toast.makeText(LoginScreen.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();
}
}
}
}で
{"email":"[email protected]","password":"123"}
こんにちは、どういうのですか? – Gwaeron
上記のコードでは、電子メールとパスワードでBufferedWriterをコーディングしています。一般的な方法でそれを作りたいと思っています。どうすればいいのですか? –
質問は「アプリにテキストを入力する方法」ですそのためにいくつかの 'EditText'を使用することができます –