我想用来jjwt
创建登录活动。所以我不知道如何使用它。我在https://github.com/jwtk/jjwt中阅读了它,但我听不懂,因为我的英语不流利。我想让你给我一个例子来理解它。我应该送任何东西去服务吗?或者我应该将密钥保存在我DatabaseHelper
的 with 中sqlite openhelper
。我OkHttp
用于将用户名和密码发送到服务并使用json
. 这是我的活动:
主要活动 :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnSignin_Clicked(View view){
String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
new SigninTask().execute(username,password);
}
public void btnSignup_Clicked(View view){
((EditText)findViewById(R.id.txtUsername)).setText("");
((EditText)findViewById(R.id.txtPassword)).setText("");
String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
new SignupTask().execute(username,password);
}
private class SignupTask extends AsyncTask<String,Object,String>{
@Override
protected String doInBackground(String... strings) {
try {
return
new OkHttpClient()
.newCall(
new Request.Builder()
.url("http://localhost:8080/Service/signup?username="+strings[0]+"&password="+strings[1])
.build()
)
.execute()
.body()
.string();
}catch (Exception e){
return null;
}
}
@Override
protected void onPostExecute(String s) {
SimpleDialog dialog=new SimpleDialog();
dialog.setContext(MainActivity.this);
dialog.setTitle("Error");
try {
JSONObject jsonObject=new JSONObject(s);
Boolean result=jsonObject.getBoolean("value");
if (result){
String username= ((EditText)findViewById(R.id.txtUsername)).getText().toString();
dialog.setMessage("Signup completed)");
((EditText)findViewById(R.id.txtUsername)).setText("");
((EditText)findViewById(R.id.txtPassword)).setText("");
}else {
((EditText)findViewById(R.id.txtUsername)).setText("");
((EditText)findViewById(R.id.txtPassword)).setText("");
dialog.setMessage("Sign up failed !");
}
}catch (Exception e){
dialog.setMessage("Error occured !");
}
dialog.show(getSupportFragmentManager(),"dialog");
}
}
private class SigninTask extends AsyncTask<String,Object,String>{
@Override
protected String doInBackground(String... strings) {
try {
return new OkHttpClient()
.newCall(
new Request.Builder()
.url("http://localhost:8080/Service/authorize?username="+strings[0]+"&password="+strings[1])
.build()
)
.execute()
.body()
.string();
}catch (Exception e){
return null;
}
}
@Override
protected void onPostExecute(String s) {
SimpleDialog dialog=new SimpleDialog();
dialog.setContext(MainActivity.this);
dialog.setTitle("Error");
try {
JSONObject jsonObject=new JSONObject(s);
Boolean result=jsonObject.getBoolean("value");
if (result){
String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
Intent intent=new Intent(MainActivity.this,CategoryActivity.class);
User user=new User();
user.setUsername(username);
user.setPassword(password);
intent.putExtra("user",user);
startActivity(intent);
finish();
}else {
dialog.setMessage("Invalid username or password !");
}
}catch (Exception e){
dialog.setMessage("Error occured !");
}
dialog.show(getSupportFragmentManager(),"dialog");
}
}
}