private String usernameApi;
private String passwordApi;
private EditText usernameet;
private EditText passwordet;
private Button login_btn;
private List<User> users = new ArrayList<User>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameet = findViewById(R.id.username_etext);
passwordet = findViewById(R.id.password_etext);
login_btn = findViewById(R.id.login_btn);
getRetrofitArray();
}
从Restful服务获取JSONArray的方法
void getRetrofitArray() {
String BASE_URL =
"http://192.168.0.18:8080/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RestAPI service = retrofit.create(RestAPI.class);
Call<JsonArray> jsonCall = service.getUsers();
jsonCall.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
String userData = response.body().toString();
Log.i("onResponse", userData);
Type listType = new TypeToken<List<User>>() {}.getType();
List<User> users = new Gson().fromJson(userData, listType);
Log.i("onResponse", users.toString());
for(int i = 0; i < users.size(); i++) {
usernameApi = users.get(i).getUsername();
passwordApi = users.get(i).getPassword();
}
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login(usernameet.getText().toString(), passwordet.getText().toString());
}
});
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
}
});
}
最后是登录方法
public void login(String usernameInput, String passwordInput) {
if ((usernameInput.equals(usernameApi)) && (passwordInput.equals(passwordApi))) {
Intent i = new Intent(this, PostsActivity.class);
startActivity(i);
finish();
}else{
login_btn.setEnabled(false);
}
}
来自回复的userData是[{“id”:1,“name”:“Radovan”,“username”:“1”,“password”:“1”,“photo”:“slika”}, { “ID”:2, “名”: “米洛”, “用户名”: “2”, “密码”: “2”, “照片”: “SLIKA”}, { “ID”:3 “名称”: “Zivan”, “用户名”: “3”, “密码”: “3”, “照片”: “SLIKA”}]
答案 0 :(得分:0)
定义此变量:
private List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
试试这段代码:
private String usernameApi;
private String passwordApi;
private EditText usernameet;
private EditText passwordet;
private Button login_btn;
private List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
usernameet = findViewById(R.id.username_etext);
passwordet = findViewById(R.id.password_etext);
login_btn = findViewById(R.id.login_btn);
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login(usernameet.getText().toString(), passwordet.getText().toString());
}
});
getRetrofitArray();
}
和
void getRetrofitArray() {
String BASE_URL = "http://192.168.0.18:8080/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RestAPI service = retrofit.create(RestAPI.class);
Call<JsonArray> jsonCall = service.getUsers();
jsonCall.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
String userData = response.body().toString();
listOfMaps = new ArrayList<Map<String, String>>();
JSONArray temp = new JSONArray(userData);
for (int i = 0; i < temp.length(); i++) {
JSONObject row = (JSONObject) temp.get(i);
int id = row.getInt("id");
String name = row.getString("name");
String username = row.getString("username");
String password = row.getString("password");
String photo = row.getString("photo");
HashMap<String, String> user_exist = new HashMap<String, String>();
user_exist.put("id",String.valueOf("id"));
user_exist.put("name",name);
user_exist.put("username",username);
user_exist.put("password",password);
user_exist.put("photo",photo);
listOfMaps.add(user_exist);
}
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
}
});
}
功能:
public void login(String usernameInput, String passwordInput) {
boolean exist = false;
try{
for(int i=0; i<= listOfMaps.size(); i++){
if(listOfMaps.get(i).get("username").equals(usernameInput) && listOfMaps.get(i).get("password").equals(passwordInput)){
exist = true;
// save in shared preferences
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", listOfMaps.get(i).get("username"));
editor.putString("password", listOfMaps.get(i).get("password"));
editor.putString("id", listOfMaps.get(i).get("id") );
editor.putString("photo", listOfMaps.get(i).get("photo") );
editor.commit();
break;
}
}
}catch (Exception ex){
ex.printStackTrace();
}
if(exist){
Intent i = new Intent(this, PostsActivity.class);
startActivity(i);
finish();
}else {
login_btn.setEnabled(false);
}
}
要检索其他活动中的数据,请在PostsActivity中尝试此代码:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(PostsActivity.this);
String username = sp.getString("id_user", "");
String password = sp.getString("password", "");
String id = sp.getString("id", "");
String photo = sp.getString("photo", "");