OkHttp请求
来源:互联网 发布:为什么要学c语言 编辑:程序博客网 时间:2024/06/11 02:01
public class ShoppingActivity extends BaseActivity implements View.OnClickListener { private List<ShoppingBean.DataBean> list = new ArrayList<>(); private EditText et; private TextView change; private RecyclerView rv; private boolean flag; private TextView find; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shopping); initView(); } private void initView() { et = (EditText) findViewById(R.id.et); change = (TextView) findViewById(R.id.change); rv = (RecyclerView) findViewById(R.id.rv); change.setOnClickListener(this); find = (TextView) findViewById(R.id.find); find.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.change: if(!flag){ show(false); flag=true; }else{ show(true); flag=false; } break; case R.id.find: String s = et.getText().toString().trim(); httpUtil.doGet("http://120.27.23.105/product/searchProducts?keywords=" + s + "&page=1", null, null, ShoppingBean.class, new OnNetListener() { @Override public void onSuccess(BaseBean baseBean) throws IOException { ShoppingBean shoppingBean = (ShoppingBean) baseBean; list = shoppingBean.getData(); show(true); } @Override public void onError(IOException e) { } }); break; } } public void show(boolean flag){ rv.setLayoutManager(flag ? new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false) : new GridLayoutManager(this,2)); MyAdapter adapter = new MyAdapter(this,list); rv.setAdapter(adapter); }}public class MyMainActivity extends BaseActivity implements View.OnClickListener { private HashMap<String,String> params = new HashMap<>(); private TextView user; private TextView name; private Button finish; private Button shopping; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_main); initView(); Intent intent = getIntent(); int uid = intent.getIntExtra("UID",0); httpUtil.doGet("http://120.27.23.105/user/getUserInfo?uid="+uid,null,null, MyMainBean.class, new OnNetListener() { @Override public void onSuccess(BaseBean baseBean) throws IOException { MyMainBean myMainBean = (MyMainBean) baseBean; String username = myMainBean.getData().getUsername(); String password = myMainBean.getData().getPassword(); user.setText(username); name.setText(password); Toast.makeText(MyMainActivity.this,"请求成功",Toast.LENGTH_SHORT).show(); } @Override public void onError(IOException e) { Toast.makeText(MyMainActivity.this,"请求失败",Toast.LENGTH_SHORT).show(); } }); } private void initView() { user = (TextView) findViewById(R.id.user); name = (TextView) findViewById(R.id.name); finish = (Button) findViewById(R.id.finish); shopping = (Button) findViewById(R.id.shopping); finish.setOnClickListener(this); shopping.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.finish: MyMainActivity.this.finish(); break; case R.id.shopping: Intent intent = new Intent(MyMainActivity.this,ShoppingActivity.class); startActivity(intent); break; }public class BaseActivity extends AppCompatActivity { private LinkedList<Activity> list = new LinkedList<>(); protected HttpUtil httpUtil; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); list.add(this); httpUtil = HttpUtil.getInstance(this); } public void finishAll(){ for (Activity ac : list){ if(!ac.isFinishing()){ ac.finish(); } } }} }}public class RegisterActivity extends BaseActivity implements View.OnClickListener { private Button register; private EditText re_phone; private EditText re_pwd; private HashMap<String,String> params = new HashMap<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); initView(); } private void initView() { re_phone = (EditText) findViewById(R.id.re_phone); re_pwd = (EditText) findViewById(R.id.re_pwd); register = (Button) findViewById(R.id.register); register.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.register: String mobile = re_phone.getText().toString().trim(); String password = re_pwd.getText().toString().trim(); params.put("mobile",mobile); params.put("password",password); httpUtil.doPost(Api.REGISTER, params, RegisterBean.class, new OnNetListener() { @Override public void onSuccess(BaseBean baseBean) throws IOException { Toast.makeText(RegisterActivity.this,"注册成功!",Toast.LENGTH_SHORT).show(); RegisterActivity.this.finish(); } @Override public void onError(IOException e) { Toast.makeText(RegisterActivity.this,"注册失败!",Toast.LENGTH_SHORT).show(); } }); break; } }}public class MainActivity extends BaseActivity implements View.OnClickListener { private Button bt_zhuce; private EditText phone; private EditText pwd; private Button bt_login; private HashMap<String,String> params = new HashMap<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { phone = (EditText) findViewById(R.id.phone); pwd = (EditText) findViewById(R.id.pwd); bt_zhuce = (Button) findViewById(R.id.bt_zhuce); bt_zhuce.setOnClickListener(this); bt_login = (Button) findViewById(R.id.bt_login); bt_login.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_zhuce: Intent intent = new Intent(MainActivity.this,RegisterActivity.class); startActivity(intent); break; case R.id.bt_login: String mobile = phone.getText().toString().trim(); String password = pwd.getText().toString().trim(); params.put("mobile",mobile); params.put("password",password); httpUtil.doPost(Api.LOGIN, params, LoginBean.class, new OnNetListener() { @Override public void onSuccess(BaseBean baseBean) throws IOException { LoginBean loginBean = (LoginBean) baseBean; int uid = loginBean.getData().getUid(); Intent intent = new Intent(MainActivity.this,MyMainActivity.class); intent.putExtra("UID",uid); startActivity(intent); MainActivity.this.finish(); } @Override public void onError(IOException e) { Toast.makeText(MainActivity.this,"登录失败!",Toast.LENGTH_SHORT).show(); } }); break; } }}public class MyAdapter extends RecyclerView.Adapter{ private Context context; private List<ShoppingBean.DataBean> list; private static final int TYPE1 = 0; private static final int TYPE2 = 1; public MyAdapter(Context context, List<ShoppingBean.DataBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RecyclerView.ViewHolder holder = null; if(viewType == TYPE1){ View view1 = LayoutInflater.from(context).inflate(R.layout.item1, parent, false); holder = new Type1ViewHolder(view1); }else{ View view2 = LayoutInflater.from(context).inflate(R.layout.item2, parent, false); holder = new Type2ViewHolder(view2); } return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if(holder instanceof Type1ViewHolder){ ShoppingBean.DataBean dataBean = list.get(position); Type1ViewHolder type1ViewHolder = (Type1ViewHolder) holder; type1ViewHolder.item1_price.setText(dataBean.getPrice()+""); type1ViewHolder.item1_title.setText(dataBean.getTitle()); ImageLoader.getInstance().displayImage(dataBean.getImages(),type1ViewHolder.item1_iv); }else{ ShoppingBean.DataBean dataBean = list.get(position); Type2ViewHolder type2ViewHolder = (Type2ViewHolder) holder; type2ViewHolder.item2_price.setText(dataBean.getPrice()+""); type2ViewHolder.item2_title.setText(dataBean.getTitle()); ImageLoader.getInstance().displayImage(dataBean.getImages(),type2ViewHolder.item2_iv); } } @Override public int getItemViewType(int position) { return position % 2 == 0? TYPE1 : TYPE2; } @Override public int getItemCount() { return list.size(); } private class Type1ViewHolder extends RecyclerView.ViewHolder{ private final ImageView item1_iv; private final TextView item1_title; private final TextView item1_price; public Type1ViewHolder(View itemView) { super(itemView); item1_iv = itemView.findViewById(R.id.item1_iv); item1_title = itemView.findViewById(R.id.item1_title); item1_price = itemView.findViewById(R.id.item1_price); } } private class Type2ViewHolder extends RecyclerView.ViewHolder{ private final ImageView item2_iv; private final TextView item2_title; private final TextView item2_price; public Type2ViewHolder(View itemView) { super(itemView); item2_iv = itemView.findViewById(R.id.item2_iv); item2_title = itemView.findViewById(R.id.item2_title); item2_price = itemView.findViewById(R.id.item2_price); } }}public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); }}
阅读全文
0 0
- OKhttp请求
- OkHttp请求
- okhttp请求
- okhttp GET请求 post请求
- OkHttp Get请求/Post请求
- java okhttp 网络请求
- okhttp请求Demo
- OKhttp post请求
- OkHttp各种请求方法
- OKHttp 请求库
- OkHttp网络请求
- OKHttp请求网络数据
- okHttp post&get请求
- okhttp网络请求框架
- OkHttp Post请求方式
- 网络请求框架OKHttp
- OkHttp之get请求
- OkHttp网络请求
- 一个oracle 用户下面创建多个实例
- 小型的Unix系统字符SHELL
- c++中的各种类型转换
- Codeforces Round #440 (Div. 2)C. Maximum splitting
- angularjs 初使用
- OkHttp请求
- BP神经网络的matlab实现
- 表单验证
- 10.15PCA
- angularjs 初使用2
- Codeforces Round #440 div2 A. Search for Pretty Integers
- 【数论】[HNOI2006]鬼谷子的钱袋
- angularjs $http请求--图书
- Codeforces Round #440 (Div. 1) B. Something with XOR Query