//MainActivity
public class MainActivity extends AppCompatActivity implements IViewLogin{ private TextView login_umobile; private TextView login_upwd; private JSONObject data; private LoginPresenter loginPresenter; private String umobile; private String upwd; private SharedPreferences preferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //查找控件 login_umobile = (TextView) findViewById(R.id.login_umobile); login_upwd = (TextView) findViewById(R.id.login_upwd); //创建中间者 loginPresenter = new LoginPresenter(this); //存储数据的对象 preferences = getSharedPreferences("logindata", MODE_PRIVATE); boolean islogin = preferences.getBoolean("islogin", false); if (islogin){ Intent i = new Intent(MainActivity.this,SearchActivity.class); startActivity(i); } } //登录 public void login(View view) { umobile = login_umobile.getText().toString(); upwd = login_upwd.getText().toString(); loginPresenter.getData(ApiUtil.LOGIN_URL, umobile, upwd); } //注册 public void regist(View view) { Intent intent = new Intent(MainActivity.this,RegisterActivity.class); startActivity(intent); } @Override public void onSuccess(final JSONObject obj) { runOnUiThread(new Runnable() { @Override public void run() { try { String code = obj.getString("code"); if("0".equals(code)){ //登录成功后,将数据存入SharedPreferences SharedPreferences.Editor edit = preferences.edit(); edit.putString("mobile",umobile); edit.putString("password",upwd); edit.putBoolean("islogin",true); edit.commit(); Intent intent = new Intent(MainActivity.this,SearchActivity.class); startActivity(intent); Toast.makeText(MainActivity.this,obj.getString("msg"), Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this,obj.getString("msg"), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }); } @Override public void onFailed(String error) { Log.d("MainActivity请求失败的原因",error); } }
//RegActivity
public class RegisterActivity extends AppCompatActivity implements IViewRegist{ private TextView reg_upwd; private TextView reg_umobile; private String umobile; private String upwd; private RegistPresenter registPresenter; private TextView fan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); reg_umobile = (TextView) findViewById(R.id.reg_umobile); reg_upwd = (TextView) findViewById(R.id.reg_upwd); fan = (TextView) findViewById(R.id.fan); registPresenter = new RegistPresenter(this); //点击返回 fan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } //立即注册 public void goRegist(View view) { umobile = reg_umobile.getText().toString(); upwd = reg_upwd.getText().toString(); registPresenter.getData(ApiUtil.REGIST_URL,umobile,upwd); } @Override public void onSuccess(final JSONObject obj) { runOnUiThread(new Runnable() { @Override public void run() { try { String code = obj.getString("code"); if("0".equals(code)){ Toast.makeText(RegisterActivity.this,obj.getString("msg"), Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(RegisterActivity.this,obj.getString("msg"), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }); } @Override public void onFailed(String error) { Log.d("RegActivity请求失败原因",error); } }
//SearchActivity
public class SearchActivity extends AppCompatActivity implements IViewSearch{ private EditText shu; private RecyclerView recycler_view; private SearchPresenter searchPresenter; private CheckBox ck; boolean flag = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); shu = (EditText) findViewById(R.id.shu); recycler_view = (RecyclerView) findViewById(R.id.recycler_view); ck = (CheckBox) findViewById(R.id.ck); searchPresenter = new SearchPresenter(this); } //搜索 public void search(View view) { try { String name = shu.getText().toString(); String keywords = URLEncoder.encode(name, "utf-8"); searchPresenter.getData(ApiUtil.SEARCH_URL,keywords); } catch (Exception e) { e.printStackTrace(); } } @Override public void onSuccess(final AllContent allContent) { runOnUiThread(new Runnable() { @Override public void run() { final List<AllContent.DataBean> data = allContent.getData(); Toast.makeText(SearchActivity.this, allContent.getMsg(),Toast.LENGTH_SHORT).show(); //设置适配器 /*SearchRecyclerAdapter adapter = new SearchRecyclerAdapter(SearchActivity.this,data); recycler_view.setAdapter(adapter); recycler_view.setLayoutManager(new LinearLayoutManager(SearchActivity.this, LinearLayout.VERTICAL,false));*/ setAdapter(data); ck.setChecked(flag); ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(flag){ //设置管理器、适配器 setAdapter(data); ck.setChecked(false); flag = ck.isChecked(); }else{ SearchRecyclerGVAdapter gvAdapter = new SearchRecyclerGVAdapter(SearchActivity.this, data); recycler_view.setAdapter(gvAdapter); recycler_view.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)); ck.setChecked(true); flag = ck.isChecked(); } } }); } }); } private void setAdapter(List<AllContent.DataBean> data) { SearchRecyclerAdapter adapter = new SearchRecyclerAdapter(SearchActivity.this,data); recycler_view.setAdapter(adapter); recycler_view.setLayoutManager(new LinearLayoutManager(SearchActivity.this, LinearLayout.VERTICAL,false)); } } //selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" android:drawable="@drawable/kind_grid"></item> <item android:state_checked="true" android:drawable="@drawable/kind_liner"></item> <item android:drawable="@drawable/kind_grid"></item> </selector>
//apiutil public class ApiUtil { public static final String LOGIN_URL = "https://www.zhaoapi.cn/user/login"; public static final String REGIST_URL = "https://www.zhaoapi.cn/user/reg"; public static final String SEARCH_URL = "http://120.27.23.105/product/searchProducts"; }//接口回调
//login
public class LoginModel { private IPresenterLogin iPresenterLogin; public LoginModel(IPresenterLogin iPresenterLogin) { this.iPresenterLogin = iPresenterLogin; } public void getData(String loginUrl, String umobile, String upwd) { Map<String, String> map = new HashMap<>(); map.put("mobile",umobile); map.put("password",upwd); OkHttp3Util.doPost(loginUrl, map, new Callback() { @Override public void onFailure(Call call, IOException e) { iPresenterLogin.onFailed(e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ String json = response.body().string(); if(json!=null){ //使用原生解析 try { JSONObject obj = new JSONObject(json); //使用接口回调 iPresenterLogin.onSuccess(obj); } catch (Exception e) { e.printStackTrace(); } } } } }); } }
//regist
public class RegistModel { private IPresenterRegist iPresenterRegist; public RegistModel(IPresenterRegist iPresenterRegist) { this.iPresenterRegist = iPresenterRegist; } public void getData(String loginUrl, String umobile, String upwd) { Map<String, String> map = new HashMap<>(); map.put("mobile",umobile); map.put("password",upwd); OkHttp3Util.doPost(loginUrl, map, new Callback() { @Override public void onFailure(Call call, IOException e) { iPresenterRegist.onFailed(e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ String json = response.body().string(); if(json!=null){ //使用原生解析 try { JSONObject obj = new JSONObject(json); //使用接口回调 iPresenterRegist.onSuccess(obj); } catch (Exception e) { e.printStackTrace(); } } } } }); } }
//search
public class SearchModel { private IPresenterSearch iPresenterSearch; public SearchModel(IPresenterSearch iPresenterSearch) { this.iPresenterSearch = iPresenterSearch; } public void getData(String loginUrl, String keywords) { Map<String, String> map = new HashMap<>(); Log.d("++++++++++++++",loginUrl); map.put("keywords",keywords); map.put("page","1"); map.put("source","android"); OkHttp3Util.doGet(loginUrl+"?keywords="+keywords+"&page="+1+"&source="+"android", new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ String json = response.body().string(); if(json!=null){ //使用原生解析 try { Gson gson = new Gson(); AllContent allContent = gson.fromJson(json, AllContent.class); //使用接口回调 iPresenterSearch.onSuccess(allContent); } catch (Exception e) { e.printStackTrace(); } } } } }); } } //IViewpublic interface IViewLogin { void onSuccess(JSONObject obj); void onFailed(String error); }public interface IViewRegist { void onSuccess(JSONObject obj); void onFailed(String error); } public interface IViewSearch { void onSuccess(AllContent allContent); }