MVP登录注册
来源:互联网 发布:互联网数据分析师前景 编辑:程序博客网 时间:2024/05/01 20:36
activity_main
主布局:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="lanbo.lanbo20171111.activity.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_above="@+id/rg" android:layout_height="match_parent"></android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/rg" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" > <RadioButton android:gravity="center" android:id="@+id/rb1" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="match_parent" android:button="@null" android:text="登录" android:checked="true" /> <RadioButton android:id="@+id/rb2" android:gravity="center" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="match_parent" android:button="@null" android:text="注册" /> </RadioGroup></RelativeLayout>
activity_qqlogin:
登录布局:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="lanbo.lanbo20171111.activity.QQlogin"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击QQ登录" android:onClick="buttonLogin" android:layout_centerInParent="true" android:textSize="16sp" android:textColor="#f4736e"/></RelativeLayout>
activity_song:
数据列表:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="lanbo.lanbo20171111.activity.SongActivity"><com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xrv" android:layout_width="match_parent" android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView></RelativeLayout>
activity_user:
添加布局:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="lanbo.lanbo20171111.activity.UserActivity" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="头像:" /> <ImageView android:layout_alignParentRight="true" android:id="@+id/head_iv" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher_round" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <TextView android:layout_alignParentRight="true" android:id="@+id/head_tv_name" android:layout_width="50dp" android:layout_height="50dp" android:text="asdsad" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等级" /> <TextView android:layout_alignParentRight="true" android:id="@+id/head_tv_level" android:layout_width="50dp" android:layout_height="50dp" android:text="sad" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地址:" /> <TextView android:layout_alignParentRight="true" android:id="@+id/head_tv_position" android:layout_width="50dp" android:layout_height="50dp" android:text="asdasdas" /> </RelativeLayout> <Button android:id="@+id/bt_quit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="退出登录" android:background="@android:color/holo_blue_light" android:textColor="@android:color/white" /></LinearLayout>
login_layout:
底部登录:<?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" > <TextView android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="50dp" android:text="登录界面" android:textColor="@android:color/holo_red_light" android:textSize="25dp" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入您的手机号" /> <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入您的密码" /> <Button android:id="@+id/bt_qq" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用第三方qq登录" android:background="@android:color/holo_blue_light" android:textColor="@android:color/white" /> <Button android:id="@+id/bt_log" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /></LinearLayout>
rec_layout:
注册布局:<?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" > <TextView android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="50dp" android:text="注册" android:textColor="@android:color/holo_red_light" android:textSize="25dp" /> <EditText android:id="@+id/et_rec_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入您的手机号" /> <EditText android:id="@+id/et_rec_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入您的密码" /> <Button android:id="@+id/rec_bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" android:background="@android:color/holo_blue_light" android:textColor="@android:color/white" /></LinearLayout>
head_layout:
列表内布局:<?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="100dp" android:gravity="center_horizontal" ><ImageView android:layout_width="200dp" android:layout_height="100dp" android:src="@drawable/timg" /></LinearLayout>
holder1_layout:
标题布局1:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"><ImageView android:id="@+id/item1_iv" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher_round" /> <LinearLayout android:layout_toRightOf="@id/item1_iv" android:layout_alignParentRight="true" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="vertical" android:gravity="center_horizontal" > <TextView android:id="@+id/item1_songname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="asdas" /> <TextView android:id="@+id/item1_author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="asdsadsada" /> </LinearLayout></RelativeLayout>
holder2_layout:
标题 布局2:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" > <ImageView android:layout_alignParentRight="true" android:id="@+id/item2_iv" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher_round" /> <LinearLayout android:layout_toLeftOf="@+id/item2_iv" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="vertical" android:gravity="center_horizontal" > <TextView android:id="@+id/item2_songname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="asdas" /> <TextView android:id="@+id/item2_author" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="asdsadsada" /> </LinearLayout></RelativeLayout>
Bean类分别有LoginBean,QQBean,RecBean,ShopBean:
ActivityMainActivity:package lanbo.lanbo20171111.activity;import android.os.Bundle;import android.support.annotation.IdRes;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.view.MotionEvent;import android.view.View;import android.widget.RadioGroup;import java.util.ArrayList;import java.util.List;import lanbo.lanbo20171111.R;import lanbo.lanbo20171111.fragment.FragLog;import lanbo.lanbo20171111.fragment.FragRec;public class MainActivity extends FragmentActivity { RadioGroup rg; List<Fragment> frglist = new ArrayList<>(); ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg= (RadioGroup) findViewById(R.id.rg); vp= (ViewPager) findViewById(R.id.vp); frglist.add(new FragLog()); frglist.add(new FragRec()); vp.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { return true; } }); myadp myadp=new myadp(getSupportFragmentManager()); vp.setAdapter(myadp); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { switch (i){ case R.id.rb1: vp.setCurrentItem(0); break; case R.id.rb2: vp.setCurrentItem(1); break; } } }); } class myadp extends FragmentPagerAdapter{ public myadp(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return frglist.get(position); } @Override public int getCount() { return frglist.size(); } }}
LoginAvyivity:package lanbo.lanbo20171111.activity;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Toast;import com.google.gson.Gson;import com.tencent.connect.UserInfo;import com.tencent.connect.auth.QQToken;import com.tencent.connect.common.Constants;import com.tencent.tauth.IUiListener;import com.tencent.tauth.Tencent;import com.tencent.tauth.UiError;import org.json.JSONException;import org.json.JSONObject;import lanbo.lanbo20171111.R;import lanbo.lanbo20171111.bean.QQBean;public class QQlogin extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final String APP_ID = "1105602574";//官方获取的APPID private Tencent mTencent; private BaseUiListener mIUiListener; private UserInfo mUserInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_qqlogin); //传入参数APPID和全局Context上下文 mTencent = Tencent.createInstance(APP_ID,QQlogin.this.getApplicationContext()); } public void buttonLogin(View v){ /**通过这句代码,SDK实现了QQ的登录,这个方法有三个参数,第一个参数是context上下文,第二个参数SCOPO 是一个String类型的字符串,表示一些权限 官方文档中的说明:应用需要获得哪些API的权限,由“,”分隔。例如:SCOPE = “get_user_info,add_t”;所有权限用“all” 第三个参数,是一个事件监听器,IUiListener接口的实例,这里用的是该接口的实现类 */ mIUiListener = new BaseUiListener(); //all表示获取所有权限 mTencent.login(QQlogin.this,"all", mIUiListener); } /** * 自定义监听器实现IUiListener接口后,需要实现的3个方法 * onComplete完成 onError错误 onCancel取消 */ private class BaseUiListener implements IUiListener { @Override public void onComplete(Object response) { Toast.makeText(QQlogin.this, "授权成功", Toast.LENGTH_SHORT).show(); Log.e(TAG, "response:" + response); JSONObject obj = (JSONObject) response; try { String openID = obj.getString("openid"); String accessToken = obj.getString("access_token"); String expires = obj.getString("expires_in"); mTencent.setOpenId(openID); mTencent.setAccessToken(accessToken,expires); QQToken qqToken = mTencent.getQQToken(); mUserInfo = new UserInfo(getApplicationContext(),qqToken); mUserInfo.getUserInfo(new IUiListener() { @Override public void onComplete(Object response) { Log.e(TAG,"登录成功"+response.toString()); Gson gson=new Gson(); QQBean bean = gson.fromJson(response.toString(), QQBean.class); Intent it=new Intent(QQlogin.this,UserActivity.class); it.putExtra("Icon",bean.getFigureurl_qq_2()); it.putExtra("name",bean.getNickname()); it.putExtra("lv",bean.getLevel()); it.putExtra("position",bean.getProvince()+bean.getCity()); startActivity(it); finish(); } @Override public void onError(UiError uiError) { Log.e(TAG,"登录失败"+uiError.toString()); } @Override public void onCancel() { Log.e(TAG,"登录取消"); } }); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(UiError uiError) { Toast.makeText(QQlogin.this, "授权失败", Toast.LENGTH_SHORT).show(); } @Override public void onCancel() { Toast.makeText(QQlogin.this, "授权取消", Toast.LENGTH_SHORT).show(); } } /** * 在调用Login的Activity或者Fragment中重写onActivityResult方法 * @param requestCode * @param resultCode * @param data */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == Constants.REQUEST_LOGIN){ Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener); } super.onActivityResult(requestCode, resultCode, data); }}
SongActivity:package lanbo.lanbo20171111.activity;import android.os.Bundle;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.LinearLayoutManager;import android.util.Log;import android.view.View;import android.widget.Toast;import com.jcodecraeer.xrecyclerview.XRecyclerView;import lanbo.lanbo20171111.R;import lanbo.lanbo20171111.adapter.MyAdapter;import lanbo.lanbo20171111.adapter.MyDecoration;import lanbo.lanbo20171111.bean.ShopBean;import lanbo.lanbo20171111.presenter.MyShopPresenter;import lanbo.lanbo20171111.view.IShopView;public class SongActivity extends AppCompatActivity implements IShopView { MyShopPresenter presenter=new MyShopPresenter(this,this); XRecyclerView xrv; int num=30; int offset=1; Handler handler=new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_song); xrv= (XRecyclerView) findViewById(R.id.xrv); View view=View.inflate(SongActivity.this,R.layout.head_layout,null); xrv.addHeaderView(view); xrv.addItemDecoration(new MyDecoration(this)); presenter.getSongList("30","1"); } @Override public void showshopview(final ShopBean bean) { Log.i("5555555555", "showshopview: "+bean.getSong_list().get(3).getAuthor()); LinearLayoutManager manager=new LinearLayoutManager(SongActivity.this); xrv.setLayoutManager(manager); final MyAdapter myAdapter=new MyAdapter(SongActivity.this,bean); xrv.setAdapter(myAdapter); xrv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { offset++; presenter.getSongList(num+"",offset+""); myAdapter.notifyDataSetChanged(); xrv.refreshComplete(); } },900); } @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { num+=5; presenter.getSongList(num+"",offset+""); myAdapter.notifyDataSetChanged(); xrv.loadMoreComplete(); } },900); } }); myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(SongActivity.this,bean.getSong_list().get(position).getAuthor(),Toast.LENGTH_SHORT).show(); } }); }}
UserActiivty:package lanbo.lanbo20171111.activity;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import lanbo.lanbo20171111.R;public class UserActivity extends AppCompatActivity { private ImageView mIvHead; private TextView mTvNameHead; private TextView mTvLevelHead; private TextView mTvPositionHead; private Button bt_quit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user); bt_quit= (Button) findViewById(R.id.bt_quit); initView(); Intent intent = getIntent(); String icon = intent.getStringExtra("Icon"); String name = intent.getStringExtra("name"); String lv = intent.getStringExtra("lv"); String position = intent.getStringExtra("position"); Glide.with(this) .load(icon) .into(mIvHead); mTvLevelHead.setText(lv); mTvNameHead.setText(name); mTvPositionHead.setText(position); bt_quit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent it=new Intent(UserActivity.this,MainActivity.class); startActivity(it); finish(); } }); } private void initView() { mIvHead = (ImageView) findViewById(R.id.head_iv); mTvNameHead = (TextView) findViewById(R.id.head_tv_name); mTvLevelHead = (TextView) findViewById(R.id.head_tv_level); mTvPositionHead = (TextView) findViewById(R.id.head_tv_position); }}
adapter:MyAdapter:package lanbo.lanbo20171111.adapter;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import lanbo.lanbo20171111.R;import lanbo.lanbo20171111.bean.ShopBean;/** * Created by asus on 2017/11/11. */public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{ Context context; ShopBean bean; @Override public void onClick(View v) { if (mOnItemClickListener != null) { //注意这里使用getTag方法获取position mOnItemClickListener.onItemClick(v,(int)v.getTag()); } } public static interface OnItemClickListener { void onItemClick(View view , int position); } private OnItemClickListener mOnItemClickListener = null; public MyAdapter(Context context, ShopBean bean) { this.context = context; this.bean = bean; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType==1){ View view=View.inflate(context,R.layout.holder1_layout,null); myholder1 myholder1=new myholder1(view); view.setOnClickListener(this); return myholder1; }else{ View view=View.inflate(context,R.layout.holder2_layout,null); myholder2 myholder2=new myholder2(view); view.setOnClickListener(this); return myholder2; } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if(holder instanceof myholder1){ myholder1 myholder1= (MyAdapter.myholder1) holder; Glide.with(context) .load(bean.getSong_list().get(position).getPic_small()) .into(myholder1.item1_iv); myholder1.item1_tv_author.setText(bean.getSong_list().get(position).getAuthor()); myholder1.item1_tv_songname.setText(bean.getSong_list().get(position).getTitle()); myholder1.itemView.setTag(position); }else if(holder instanceof myholder2){ myholder2 myholder2= (MyAdapter.myholder2) holder; myholder2.item2_tv_author.setText(bean.getSong_list().get(position).getAuthor()); myholder2.item2_tv_songname.setText(bean.getSong_list().get(position).getTitle()); Glide.with(context) .load(bean.getSong_list().get(position).getPic_small()) .into(myholder2.item2_iv); myholder2.itemView.setTag(position); } } @Override public int getItemCount() { return bean.getSong_list()==null?0:bean.getSong_list().size(); } @Override public int getItemViewType(int position) { if(bean.getSong_list().get(position).getTitle().length() % 2==1){ return 1; }else{ return 0; } } public void setOnItemClickListener(OnItemClickListener listener) { this.mOnItemClickListener = listener; } class myholder1 extends RecyclerView.ViewHolder{ ImageView item1_iv; TextView item1_tv_songname; TextView item1_tv_author; public myholder1(View itemView) { super(itemView); item1_iv= itemView.findViewById(R.id.item1_iv); item1_tv_songname=itemView.findViewById(R.id.item1_songname); item1_tv_author=itemView.findViewById(R.id.item1_author); } } class myholder2 extends RecyclerView.ViewHolder{ ImageView item2_iv; TextView item2_tv_songname; TextView item2_tv_author; public myholder2(View itemView) { super(itemView); item2_tv_author=itemView.findViewById(R.id.item2_author); item2_tv_songname=itemView.findViewById(R.id.item2_songname); item2_iv=itemView.findViewById(R.id.item2_iv); } }}
MyDecoration:package lanbo.lanbo20171111.adapter;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.support.v7.widget.RecyclerView;import android.view.View;import lanbo.lanbo20171111.R;/** * Created by asus on 2017/11/11. */public class MyDecoration extends RecyclerView.ItemDecoration { private int dividerHeight; private Paint dividerPaint; public MyDecoration(Context context) { dividerPaint = new Paint(); dividerPaint.setColor(context.getResources().getColor(R.color.colorAccent)); dividerHeight = context.getResources().getDimensionPixelSize(R.dimen.textandiconmargin); } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect, view, parent, state); outRect.bottom = dividerHeight; } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { int childCount = parent.getChildCount(); int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); for (int i = 0; i < childCount - 1; i++) { View view = parent.getChildAt(i); float top = view.getBottom(); float bottom = view.getBottom() + dividerHeight; c.drawRect(left, top, right, bottom, dividerPaint); } }}
Fragmeng:FragLog:package lanbo.lanbo20171111.fragment;import android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import lanbo.lanbo20171111.R;import lanbo.lanbo20171111.activity.QQlogin;import lanbo.lanbo20171111.activity.SongActivity;import lanbo.lanbo20171111.bean.LoginBean;import lanbo.lanbo20171111.presenter.MyLoginPresent;import lanbo.lanbo20171111.view.ILogInView;/** * Created by asus on 2017/11/11. */public class FragLog extends Fragment implements ILogInView { MyLoginPresent present=new MyLoginPresent(this,getActivity()); Button bt_qq; EditText et_name; EditText et_pwd; Button bt_log; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=View.inflate(getActivity(), R.layout.login_layout,null); bt_qq=view.findViewById(R.id.bt_qq); et_name=view.findViewById(R.id.et_name); et_pwd=view.findViewById(R.id.et_pwd); bt_qq.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent it=new Intent(getActivity(), QQlogin.class); startActivity(it); } }); bt_log= view.findViewById(R.id.bt_log); bt_log.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {// Toast.makeText(getActivity(),et_name.getText().toString()+"as"+et_pwd.getText().toString(),Toast.LENGTH_SHORT).show(); present.LOG(et_name.getText().toString(),et_pwd.getText().toString()); } }); return view; } @Override public void showLoginView(LoginBean bean) { Log.i("2222222222", "onSuccess: "+bean.getMsg()); Toast.makeText(getActivity(),bean.getMsg(),Toast.LENGTH_SHORT).show(); if("登录成功".equals(bean.getMsg())){ Intent it=new Intent(getActivity(),SongActivity.class); startActivity(it); } }}
FragRec:package lanbo.lanbo20171111.fragment;import android.os.Bundle;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import lanbo.lanbo20171111.R;import lanbo.lanbo20171111.bean.RecBean;import lanbo.lanbo20171111.presenter.MyRecPresenter;import lanbo.lanbo20171111.view.IRecView;/** * Created by asus on 2017/11/11. */public class FragRec extends Fragment implements IRecView, View.OnClickListener { MyRecPresenter presenter = new MyRecPresenter(getActivity(), this); private EditText mRecNameEt; private EditText mRecPwdEt; private Button mBtRec; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getActivity(), R.layout.rec_layout, null); initView(view); return view; } private void initView(@NonNull final View itemView) { mRecNameEt = (EditText) itemView.findViewById(R.id.et_rec_name); mRecPwdEt = (EditText) itemView.findViewById(R.id.et_rec_pwd); mBtRec = (Button) itemView.findViewById(R.id.rec_bt); mBtRec.setOnClickListener(this); } @Override public void getRecView(RecBean bean) { Toast.makeText(getActivity(),bean.getMsg(),Toast.LENGTH_SHORT).show(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.rec_bt: presenter.REC(mRecNameEt.getText().toString(),mRecPwdEt.getText().toString()); break; default: break; } }}
model:MyRecModel:package lanbo.lanbo20171111.model;import java.util.HashMap;import java.util.Map;import lanbo.lanbo20171111.util.OkHttp3Utils;import okhttp3.Callback;public class MyRecModel implements IRecModel { @Override public void getrecData(String phone,String pwd,Callback callback) { Map<String,String> map=new HashMap<>(); map.put("mobile",phone); map.put("password",pwd); OkHttp3Utils.doPost("http://120.27.23.105/user/reg",map,callback); }}
MySongModel:package lanbo.lanbo20171111.model;import lanbo.lanbo20171111.util.OkHttp3Utils;import okhttp3.Callback;/** * Created by asus on 2017/11/11. */public class MySongModel implements ISongModel { @Override public void getSongList(String num,String setpage,Callback callback) { OkHttp3Utils.doGet("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size="+num+"&offset="+setpage,callback); }}
ILoginModel:package lanbo.lanbo20171111.model;import okhttp3.Callback;/** * Created by asus on 2017/11/11. */public interface ILoginModel { public void getlogindata(String phone, String pwd, Callback callback);}
IRecModel :package lanbo.lanbo20171111.model;import okhttp3.Callback;/** * Created by asus on 2017/11/11. */public interface IRecModel { public void getrecData(String phone, String pwd, Callback callback);}
ISongModel:package lanbo.lanbo20171111.model;import okhttp3.Callback;/** * Created by asus on 2017/11/11. */public interface ISongModel { public void getSongList(String num,String setpage,Callback callback);}
MyLogInModel:package lanbo.lanbo20171111.model;import java.util.HashMap;import java.util.Map;import lanbo.lanbo20171111.util.OkHttp3Utils;import okhttp3.Callback;public class MyLogInModel implements ILoginModel { @Override public void getlogindata(String phone, String pwd, Callback callback) { Map<String,String> map=new HashMap<>(); map.put("mobile",phone); map.put("password",pwd); OkHttp3Utils.doPost("http://120.27.23.105/user/login",map,callback); }}
presenter:MyRecPresenter:package lanbo.lanbo20171111.presenter;import android.content.Context;import com.google.gson.Gson;import java.io.IOException;import lanbo.lanbo20171111.bean.RecBean;import lanbo.lanbo20171111.model.MyRecModel;import lanbo.lanbo20171111.util.OnUiCallback;import lanbo.lanbo20171111.view.IRecView;import okhttp3.Call;/** * Created by asus on 2017/11/11. */public class MyRecPresenter { Context context; IRecView view; MyRecModel model; public MyRecPresenter(Context context, IRecView view) { this.context = context; this.view = view; model=new MyRecModel(); } public void REC(String phone,String pwd){ model.getrecData(phone,pwd,new OnUiCallback() { @Override public void onFailed(Call call,IOException e) { } @Override public void onSuccess(String result) { Gson gson=new Gson(); RecBean bean = gson.fromJson(result,RecBean.class); view.getRecView(bean); } }); }}
MyShopPresenter :package lanbo.lanbo20171111.presenter;import android.content.Context;import com.google.gson.Gson;import java.io.IOException;import lanbo.lanbo20171111.bean.ShopBean;import lanbo.lanbo20171111.model.MySongModel;import lanbo.lanbo20171111.util.OnUiCallback;import lanbo.lanbo20171111.view.IShopView;import okhttp3.Call;/** * Created by asus on 2017/11/11. */public class MyShopPresenter { Context context; IShopView view; MySongModel model; public MyShopPresenter(Context context, IShopView view) { this.context = context; this.view = view; model=new MySongModel(); } public void getSongList(String num,String offset){ model.getSongList(num, offset, new OnUiCallback() { @Override public void onFailed(Call call, IOException e) { } @Override public void onSuccess(String result) { Gson gson=new Gson(); ShopBean bean = gson.fromJson(result, ShopBean.class); view.showshopview(bean); } }); }}
MyLoginPresent:package lanbo.lanbo20171111.presenter;import android.content.Context;import com.google.gson.Gson;import java.io.IOException;import lanbo.lanbo20171111.bean.LoginBean;import lanbo.lanbo20171111.model.MyLogInModel;import lanbo.lanbo20171111.util.OnUiCallback;import lanbo.lanbo20171111.view.ILogInView;import okhttp3.Call;/** * Created by asus on 2017/11/11. */public class MyLoginPresent { ILogInView view; Context context; MyLogInModel model; public MyLoginPresent(ILogInView view, Context context) { this.view = view; this.context = context; model=new MyLogInModel(); } public void LOG(String phone,String pwd){ model.getlogindata(phone,pwd,new OnUiCallback() { @Override public void onFailed(Call call,IOException e) { } @Override public void onSuccess(String result) { Gson gson=new Gson(); LoginBean bean = gson.fromJson(result,LoginBean.class); view.showLoginView(bean); } }); }}
util:OkHttpUtils:package lanbo.lanbo20171111.util;import android.os.Handler;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;/** * Created by lenovo on 2017/10/12. */public class OkHttpUtils { private Handler handler=new Handler(); public Handler getHandler(){ return handler; } //单例 private static OkHttpUtils okHttpUtils=new OkHttpUtils(); private OkHttpUtils(){}; public static OkHttpUtils getInstance(){ return okHttpUtils; } private OkHttpClient client; private void initOkHttpClient(){ if (client==null){ client=new OkHttpClient.Builder().build(); } } //公用的get请求方法 完成的功能不确定 public void doGet(String url, Callback callback){ initOkHttpClient(); Request request=new Request.Builder().url(url).build(); client.newCall(request).enqueue(callback); }}
OnUiCallback:package lanbo.lanbo20171111.util;import android.os.Handler;import java.io.IOException;import okhttp3.Call;import okhttp3.Callback;import okhttp3.Response;/** * Created by lenovo on 2017/10/12. */public abstract class OnUiCallback implements Callback { private Handler handler=OkHttpUtils.getInstance().getHandler(); public abstract void onFailed(Call call,IOException e); public abstract void onSuccess(String result); @Override public void onFailure(final Call call, final IOException e) { //该方法就是把 线程post到handler所在的线程 handler.post(new Runnable() { @Override public void run() {// e.getMessage() onFailed(call,e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String result=response.body().string(); handler.post(new Runnable() { @Override public void run() { onSuccess(result); } }); }}
LoggingInterceptor:package lanbo.lanbo20171111.util;import android.os.Build;import java.io.IOException;import okhttp3.Interceptor;import okhttp3.Request;import okhttp3.Response;/** * 网络拦截器 */public class LoggingInterceptor implements Interceptor { private static final String UA = "User-Agent"; @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request() .newBuilder() .addHeader(UA, makeUA()) .build(); return chain.proceed(request); } private String makeUA() { String s = Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE; return Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE; }}
OkHttp3Utils :package lanbo.lanbo20171111.util;import android.util.Log;import java.io.File;import java.io.IOException;import java.util.Map;import okhttp3.Call;import okhttp3.Callback;import okhttp3.FormBody;import okhttp3.MediaType;import okhttp3.MultipartBody;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.RequestBody;import okhttp3.Response;/** * 1. 类的用途 * 2. @author * */public class OkHttp3Utils { private static OkHttpClient okHttpClient = null; public OkHttp3Utils() { } private static OkHttpClient getOkHttpClient() { synchronized (OkHttp3Utils.class) { if (okHttpClient == null) { okHttpClient = new OkHttpClient(); } } return okHttpClient; } //上传文件 public static void loadFile(String url, File file,String fileName){ OkHttpClient okHttpClient = getOkHttpClient(); //设置文件类型 RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"),file); //设置请求体 RequestBody body = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("image",fileName,requestBody) .build(); //请求方式 Request request = new Request.Builder().url(url).post(body).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { Log.i("成功","成功"); } }); } /** * 1.接口地址 * 2.接口回调 */ public static void doGet(String url,Callback callback){ OkHttpClient okHttpClient = getOkHttpClient(); Request request = new Request.Builder() .addHeader("User-Agent","") .url(url).build(); okHttpClient.newCall(request).enqueue(callback); } /** * 1.地址 * 2.接口回调 * 3.请求体 */ public static void doPost(String url, Map<String,String> map,Callback callback){ OkHttpClient okHttpClient = getOkHttpClient(); FormBody.Builder builder = new FormBody.Builder(); //遍历map集合 设置请求体 for (String mapKey : map.keySet()){ builder.add(mapKey,map.get(mapKey)); } //设置请求方式 Request request = new Request.Builder().url(url).post(builder.build()).build(); //执行请求方式 接口回调 okHttpClient.newCall(request).enqueue(callback); } /** *1.下载地址 */ public static void doDown(String url,Callback callback){ OkHttpClient okHttpClient = getOkHttpClient(); Request build = new Request.Builder().url(url).build(); okHttpClient.newCall(build).enqueue(callback); }}
view:IRecView:package lanbo.lanbo20171111.view;import lanbo.lanbo20171111.bean.RecBean;/** * Created by asus on 2017/11/11. */public interface IRecView { public void getRecView(RecBean bean);}
IShopView :package lanbo.lanbo20171111.view;import lanbo.lanbo20171111.bean.ShopBean;/** * Created by asus on 2017/11/11. */public interface IShopView { public void showshopview(ShopBean bean);}
ILogInView:package lanbo.lanbo20171111.view;import lanbo.lanbo20171111.bean.LoginBean;/** * Created by asus on 2017/11/11. */public interface ILogInView { public void showLoginView(LoginBean bean);}
阅读全文
0 0
- MVP 实现登录注册
- 登录注册mvp模式
- MVP注册登录
- MVP 登录注册
- MVP注册登录
- MVP 登录+注册
- MVP登录注册
- MVP登录注册
- MVP登录注册
- Mvp实现登录注册
- MVP登录注册跳转
- MVP登录注册
- MVP登录注册、跳转
- MVP实现登录注册
- 登录注册mvp
- 登录注册(MVp)
- MVP注册登录
- MVP登录注册
- CS50 2017 Lecture 0
- 苏嵌学习的第一天
- 2018网易校招内推--彩色砖块
- Recyclerview布局多条目加载+上下拉
- 属性动画模版
- MVP登录注册
- 侧滑菜单的使用——
- LintCode 二叉树的路径和
- [RE]一个RE题
- easyui 与 java 通信问题
- 初中生如何入门编程
- Spring 中常用的几种注解
- pta Percolate Up and Down
- PADS设置敷铜实心铜和网格铜