MainActivity
package com.mvp.loginmvp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, UserPresenter.OnUserPresenter {
private EditText mUsername;
private EditText mPassword;
private Button login;
Dao dao;
String urlstring = "http://i.jandan.net/?oxwlxojflwblxbsapi=get_recent_posts&include=url,date,tags,author,title,excerpt,comment_count,comment_status,custom_fields&page=1&custom_fields=thumb_c,views&dev=1";
private UserPresenter mUserPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
dao = new Dao(this);
UserPresenter.loadData(urlstring,dao);
mUserPresenter = new UserPresenter(this);
}
//初始化资源控件
private void initView() {
mUsername = findViewById(R.id.username);
mPassword = findViewById(R.id.password);
login = findViewById(R.id.login);
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
//获取账号与密码
String username = mUsername.getText().toString().trim();
String password = mPassword.getText().toString().trim();
User user = new User();
user.username = username;
user.password = password;
boolean b = UserPresenter.kong(user);
if (b){
Toast.makeText(this, "账号或者密码不能为空", Toast.LENGTH_SHORT).show();
}else{
mUserPresenter.gets(dao,user);
}
break;
}
}
@Override
public void success() {
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}
@Override
public void error() {
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
User类
package com.mvp.loginmvp;
/**
* date:2018/11/9
* author:李壮(HUAWEI)
* function:
*/
public class User {
public String username;
public String password;
}
创建Presenter
package com.mvp.loginmvp;
import android.os.AsyncTask;
import android.text.TextUtils;
import java.util.List;
/**
* date:2018/11/9
* author:李壮(HUAWEI)
* function:
*/
public class UserPresenter {
public UserPresenter(OnUserPresenter onUserPresenter) {
mOnUserPresenter = onUserPresenter;
}
//加载网络数据
public static void loadData(String urlstring, final Dao dao){
new AsyncTask<String, Void, List<Bean.PostsBean>>() {
Bean bean = null;
@Override
protected List<Bean.PostsBean> doInBackground(String... strings) {
bean = HTTPURL.gethttp(strings[0],Bean.class);
return bean.getPosts();
}
@Override
protected void onPostExecute(List<Bean.PostsBean> postsBeans) {
super.onPostExecute(postsBeans);
for (int i =0;i<postsBeans.size();i++){
dao.add(postsBeans.get(i).getId()+"",postsBeans.get(i).getId()+"");
}
}
}.execute(urlstring);
}
//判断用户输入的账号和密码是否为空
public static boolean kong(User user) {
if (TextUtils.isEmpty(user.username) || TextUtils.isEmpty(user.password)){
return true;
}
return false;
}
public void gets(Dao dao, User user){
boolean select = dao.select(user.username, user.password);
if (select){
mOnUserPresenter.success();
}else{
mOnUserPresenter.error();
}
}
public interface OnUserPresenter{
void success();
void error();
}
private OnUserPresenter mOnUserPresenter;
}
创建数据库 , 创建数据库表
package com.mvp.loginmvp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
/**
* date:2018/11/9
* author:李壮(HUAWEI)
* function:
*/
public class SQLite extends SQLiteOpenHelper {
//创建数据库
public SQLite( Context context) {
super(context, "users.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
db.execSQL("create table users(id Integer, name text,password text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
创建 数据库的增删该查类
package com.mvp.loginmvp;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
* date:2018/11/9
* author:李壮(HUAWEI)
* function:
*/
public class Dao {
private final SQLiteDatabase database;
public Dao(Context context) {
SQLite sqLite = new SQLite(context);
database = sqLite.getReadableDatabase();
}
public void add(String name,String password){
//添加语句
database.execSQL("insert into users(name,password) values(?,?)",new Object[]{name,password});
}
public boolean select(String name,String password){
//查询语句
Cursor cursor = database.rawQuery("select * from users where name = ? and password = ?", new String[]{name, password});
while (cursor.moveToNext()){
return true;
}
return false;
}
}
封装网络请求工具类
package com.mvp.loginmvp;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* date:2018/11/9
* author:李壮(HUAWEI)
* function:
*/
public class HTTPURL {
public static <T> T gethttp(String url,Class c) {
String ss= getString(url);
T t = (T) new Gson().fromJson(ss,c);
return t;
}
private static String getString(String urlString) {
try {
//1.获取地址
URL url = new URL(urlString);
//2.打开连接
HttpURLConnection coon = (HttpURLConnection) url.openConnection();
//3.设置请求方法
coon.setRequestMethod("GET");
//4.设置连接超时
coon.setConnectTimeout(5000);
//5.设置读取超时
coon.setReadTimeout(5000);
//6.请求结果
if (coon.getResponseCode() == 200){
String ss = stream2String(coon.getInputStream());
return ss;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String stream2String(InputStream is) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
for (String temp = reader.readLine() ; temp !=null;temp = reader.readLine()){
sb.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
布局文件走一波
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"/>
<EditText
android:id="@+id/username"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"/>
<EditText
android:id="@+id/password"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="登录"
/>
</LinearLayout>
添加请求网络权限
创建Bean类
package com.mvp.loginmvp;
import java.util.List;
/**
* date:2018/11/9
* author:李壮(HUAWEI)
* function:
*/
public class Bean {
private String status;
private int count;
private int count_total;
private int pages;
private List<PostsBean> posts;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCount_total() {
return count_total;
}
public void setCount_total(int count_total) {
this.count_total = count_total;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public List<PostsBean> getPosts() {
return posts;
}
public void setPosts(List<PostsBean> posts) {
this.posts = posts;
}
public static class PostsBean {
private int id;
private String url;
private String title;
private String excerpt;
private String date;
private AuthorBean author;
private int comment_count;
private String comment_status;
private CustomFieldsBean custom_fields;
private List<TagsBean> tags;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getExcerpt() {
return excerpt;
}
public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public AuthorBean getAuthor() {
return author;
}
public void setAuthor(AuthorBean author) {
this.author = author;
}
public int getComment_count() {
return comment_count;
}
public void setComment_count(int comment_count) {
this.comment_count = comment_count;
}
public String getComment_status() {
return comment_status;
}
public void setComment_status(String comment_status) {
this.comment_status = comment_status;
}
public CustomFieldsBean getCustom_fields() {
return custom_fields;
}
public void setCustom_fields(CustomFieldsBean custom_fields) {
this.custom_fields = custom_fields;
}
public List<TagsBean> getTags() {
return tags;
}
public void setTags(List<TagsBean> tags) {
this.tags = tags;
}
public static class AuthorBean {
private int id;
private String slug;
private String name;
private String first_name;
private String last_name;
private String nickname;
private String url;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public static class CustomFieldsBean {
private List<String> thumb_c;
public List<String> getThumb_c() {
return thumb_c;
}
public void setThumb_c(List<String> thumb_c) {
this.thumb_c = thumb_c;
}
}
public static class TagsBean {
private int id;
private String slug;
private String title;
private String description;
private int post_count;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPost_count() {
return post_count;
}
public void setPost_count(int post_count) {
this.post_count = post_count;
}
}
}
}