登录+二维码

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yin_chenglong/article/details/80397395


主页面

package com.example.yinchenglong1601r20180521.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.yinchenglong1601r20180521.R;

public class MainActivity extends AppCompatActivity {

    private EditText uname;
    private EditText psw;
    private Button tj;
    private CheckBox cb1;
    private CheckBox cb2;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        initView();
        //使用键值对来判断是否选中记住密码和自动登录
        if (sharedPreferences.getBoolean("RememberPassword", false)) {
            cb1.setChecked(true);
            uname.setText(sharedPreferences.getString("username", ""));
            psw.setText(sharedPreferences.getString("password", ""));
        }
        if (sharedPreferences.getBoolean("AutomaticLogin", false)) {
            cb2.setChecked(true);
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);

            startActivity(intent);
        }
        jizhumima();

    }

    private void jizhumima() {
        //选中记住密码时
        cb1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cb1.isChecked()) {
                    sharedPreferences.edit().putBoolean("RememberPassword", true).commit();
                } else {
                    sharedPreferences.edit().putBoolean("RememberPassword", false).commit();
                }

            }
        });
        //选中自动登录时也选中记住密码
        cb2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cb2.isChecked()) {
                    cb1.setChecked(true);
                    sharedPreferences.edit().putBoolean("AutomaticLogin", true).commit();
                } else {
                    sharedPreferences.edit().putBoolean("AutomaticLogin", false).commit();
                }
            }
        });
    }

    private void initView() {
        //获取id
        uname = findViewById(R.id.et1);
        psw = findViewById(R.id.et2);
        tj = findViewById(R.id.tijiao);
        cb1 = findViewById(R.id.cb1);
        cb2 = findViewById(R.id.cb2);
        //使用SharedPreferences方法
        sharedPreferences = getSharedPreferences("loginUser", Context.MODE_PRIVATE);
        //点击跳转
        tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断文本框
                login(v);
                //判断是否选中
                if (cb1.isChecked()) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("username", uname.getText().toString());
                    editor.putString("password", psw.getText().toString());
                    editor.commit();

                } else {
                    uname.setText("");
                    psw.setText("");
                }
            }
        });
    }

    public void login(View view){
        String name = uname.getText().toString();//用户名
        String pwd = psw.getText().toString();//密码

        StringBuilder stringBuilder = new StringBuilder();
        boolean flag = true;
        if ("".equals(name) || "".equals(name.trim())
                || name == null) {
            stringBuilder.append("手机号不能为空");
            flag = false;
        } else {

            if (name.length() != 11) {
                stringBuilder.append("手机号长度为11位有效数字");
                flag = false;
            }
            if (!name.startsWith("1")) {
                if (stringBuilder.length() > 0) {
                    stringBuilder.append(",手机号应该以数字1开头");
                } else {
                    stringBuilder.append("手机号应该以数字1开头");
                }

                flag = false;
            }
        }
        if ("".equals(pwd) || "".equals(pwd.trim())
                || pwd == null) {
            if (stringBuilder.length() > 0) {
                stringBuilder.append(",密码不能为空");
            } else {
                stringBuilder.append("密码不能为空");
            }
            flag = false;
        }else if(pwd.length()!=6){
            if (stringBuilder.length() > 0) {
                stringBuilder.append(",密码长度不正确");
            } else {
                stringBuilder.append("密码长度不正确");
            }
            flag = false;
        }
        if (flag == false) {
            Toast.makeText(MainActivity.this, stringBuilder.toString(),
                    Toast.LENGTH_SHORT).show();
        }else {
            Intent intent=new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
            finish();
        }

    }
}

主页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yinchenglong1601r20180521.view.MainActivity">

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="用户名:"/>
       <EditText
           android:layout_width="180dp"
           android:layout_height="wrap_content"
           android:id="@+id/et1"/>
   </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"/>
        <EditText
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:password="true"
            android:id="@+id/et2"/>
    </LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cb1"
        android:text="记住密码"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cb2"
        android:text="自动登录"/>
</LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"
        android:id="@+id/tijiao"/>
</LinearLayout>

第二个页面

package com.example.yinchenglong1601r20180521.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.example.yinchenglong1601r20180521.R;
import com.example.yinchenglong1601r20180521.presenter.Fragment01;
import com.example.yinchenglong1601r20180521.presenter.Fragment02;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {

    private RadioGroup rg;
    private FrameLayout fl;
    private List<Fragment> list;
    private Fragment01 fragment01;
    private Fragment02 fragment02;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //获取id
        rg = findViewById(R.id.rg);
        fl = findViewById(R.id.fl);
        //定义一个大集合
        list = new ArrayList<>();
        //实例化对象
        fragment01 = new Fragment01();
        fragment02 = new Fragment02();
        //添加到集合中
        list.add(fragment01);
        list.add(fragment02);
        //进行点击切换的前提
        final FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl,list.get(0)).commit();
        //点击事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            //根据点击的按钮id来判断展示的内容
            switch (i){
                case R.id.rb1:
                    fragmentManager.beginTransaction().replace(R.id.fl,fragment01).commit();
                    break;
                case R.id.rb2:
                    fragmentManager.beginTransaction().replace(R.id.fl,fragment02).commit();
                    break;
            }
        }
    });
    }
}

第二个页面布局



fragment01

package com.example.yinchenglong1601r20180521.presenter;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
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.ListView;

import com.example.yinchenglong1601r20180521.R;
import com.example.yinchenglong1601r20180521.model.MyBean;
import com.example.yinchenglong1601r20180521.model.NetWorkUtils;
import com.example.yinchenglong1601r20180521.view.MyAdapter;
import com.google.gson.Gson;

import java.util.List;

/**
 * data:2018/05/21.
 * author : 殷成龙(Administrator)
 * function :
 */

public class Fragment01 extends Fragment{

    private ListView lv;
    private MyAdapter adapter;
    private List<MyBean.DataBean> list;
    String path = "http://www.xieast.com/api/news/news.php";
    @SuppressLint("StaticFieldLeak")
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment01, container, false);
        //获取id
        lv = view.findViewById(R.id.lv);
        //new Asy();

        new AsyncTask<String,Integer,String>(){


            @Override
            protected String doInBackground(String... params) {
                NetWorkUtils netWorkUtils = new NetWorkUtils();
                String url = netWorkUtils.getURL(path);
                return url;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                MyBean myBean = gson.fromJson(s, MyBean.class);
                list = myBean.getData();
                adapter = new MyAdapter(list,getActivity());
                lv.setAdapter(adapter);

            }
        }.execute();
        //设置适配器

        return view;
    }

}

二维码界面

fragment02

package com.example.yinchenglong1601r20180521.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
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.ImageView;

import com.example.yinchenglong1601r20180521.R;
import com.example.yinchenglong1601r20180521.view.MainActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * data:2018/05/21.
 * author : 殷成龙(Administrator)
 * function :
 */

public class Fragment02 extends Fragment{

    private Button btn;
    private ImageView img;
    private Bitmap bitmap;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment02, container, false);
        btn = view.findViewById(R.id.btn);
        img = view.findViewById(R.id.img);
        //生成二维码
        bitmap = CodeUtils.createImage("15011444890", 400, 400, null);
        img.setImageBitmap(bitmap);
        btn.setOnClickListener(new View.OnClickListener() {

            private SharedPreferences sharedPreferences;

            @Override
            public void onClick(View v) {
                //点击时把自动登录变为false
                sharedPreferences = getActivity().getSharedPreferences("loginUser", Context.MODE_PRIVATE);
                sharedPreferences.edit().putBoolean("AutomaticLogin", false).commit();
                //跳转到登录页面
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }
}

fragmen01布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv"></ListView>
</android.support.constraint.ConstraintLayout>

fragment01的item布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/img1"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/img2"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/img3"/>
</LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"/>
</LinearLayout>

fragment02布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/img"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="退出登陆"
        />
</LinearLayout>

MyAdapter适配器

package com.example.yinchenglong1601r20180521.view;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yinchenglong1601r20180521.R;
import com.example.yinchenglong1601r20180521.model.MyBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * data:2018/05/21.
 * author : 殷成龙(Administrator)
 * function :
 */

public class MyAdapter extends BaseAdapter {
    private List<MyBean.DataBean> list;
    private Context context;
    private ViewHolder holder;

    public MyAdapter(List<MyBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = View.inflate(context, R.layout.item,null);
            holder = new ViewHolder();
            holder.tv = convertView.findViewById(R.id.tv);
            holder.img1 = convertView.findViewById(R.id.img1);
            holder.img2 = convertView.findViewById(R.id.img2);
            holder.img3 = convertView.findViewById(R.id.img3);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv.setText(list.get(position).getTitle());
        ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.img1);
        Log.d("msg",list.get(position).getThumbnail_pic_s());
        ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder.img2);
        ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),holder.img3);

        return convertView;
    }
    class ViewHolder{
        ImageView img1,img2,img3;
        TextView tv;
    }
}

图片工具类

package com.example.yinchenglong1601r20180521.model;

import android.app.Application;
import android.os.Environment;

import com.example.yinchenglong1601r20180521.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.io.File;

/**
 * data:2018/05/21.
 * author : 殷成龙(Administrator)
 * function :图片工具类
 */

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        File file = new File(Environment.getExternalStorageDirectory() + "/" + "image");
        ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this)
                //设置缓存地址
                .diskCache(new UnlimitedDiskCache(file))
                //关联getimg
                .defaultDisplayImageOptions(getimg())
                //内存缓存20M
                .memoryCacheSize(2 * 1024 * 1024)
                //可以缓存文件数量
                .diskCacheFileCount(100)
                //线程池数量
                .threadPoolSize(3)
                .build();
        ImageLoader.getInstance().init(build);
    }

    public DisplayImageOptions getimg() {
        DisplayImageOptions img = new DisplayImageOptions.Builder()
                //加载时显示的图片
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                //设置圆角
                .displayer(new RoundedBitmapDisplayer(25))
                .build();
        return img;
    }
}

bean类

package com.example.yinchenglong1601r20180521.model;

import java.util.List;

/**
 * data:2018/05/21.
 * author : 殷成龙(Administrator)
 * function :
 */

public class MyBean {

    /**
     * code : 1
     * data : [{"author_name":"大国军情","category":"头条","date":"2018-05-12 07:25","thumbnail_pic_s":"http://04.imgmini.eastday.com/mobile/20180512/20180512_fe1bf1b6ec00098f9455c84dc81e6763_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http://04.imgmini.eastday.com/mobile/20180512/20180512_3f636fca330f2311583896aed3185196_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http://04.imgmini.eastday.com/mobile/20180512/20180512_793fab03dadcd417bf2d8e5e1e48a0f5_cover_mwpm_03200403.jpg","title":"美国被特朗普坑惨了!又有3个铁杆盟友公开唱对台戏!","uniquekey":"1326bf3c4505783d4a964f81b6220e9d","url":"http://mini.eastday.com/mobile/180512072544020.html"},{"author_name":"新华网","category":"头条","date":"2018-05-12 07:25","thumbnail_pic_s":"http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_5_mwpm_03200403.jpg","thumbnail_pic_s02":"http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_1_mwpm_03200403.jpg","thumbnail_pic_s03":"http://01.imgmini.eastday.com/mobile/20180512/20180512072505_0fe08f494e7c090764244e3581b3e5ca_3_mwpm_03200403.jpg","title":"第71届戛纳电影节:贾樟柯电影《江湖儿女》首映","uniquekey":"9db153b507c10bbbed1d26c29c030eef","url":"http://mini.eastday.com/mobile/180512072505095.html"},{"author_name":"遇见优佳","category":"头条","date":"2018-05-12 07:24","thumbnail_pic_s":"http://06.imgmini.eastday.com/mobile/20180512/20180512_38f5183808987be3783b180740d12a2a_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http://06.imgmini.eastday.com/mobile/20180512/20180512_a3d9f98eb0e3fe36574581f00fa0f898_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http://06.imgmini.eastday.com/mobile/20180512/20180512_37fa1339af2e64cfd2dc85191950e8ba_cover_mwpm_03200403.jpg","title":"以色列发射60枚火箭弹对伊朗进行报复,中东小霸王称号保不住了","uniquekey":"4f47eb31b188cfa90881260bb2b11aa8","url":"http://mini.eastday.com/mobile/180512072412903.html"},{"author_name":"国际在线","category":"头条","date":"2018-05-12 07:22","thumbnail_pic_s":"http://00.imgmini.eastday.com/mobile/20180512/20180512072229_a296d945ae7fe08eed7d78060ce3f5c2_6_mwpm_03200403.jpg","thumbnail_pic_s02":"http://00.imgmini.eastday.com/mobile/20180512/20180512072229_a296d945ae7fe08eed7d78060ce3f5c2_7_mwpm_03200403.jpg","thumbnail_pic_s03":"http://00.imgmini.eastday.com/mobile/20180512/20180512072229_a296d945ae7fe08eed7d78060ce3f5c2_5_mwpm_03200403.jpg","title":"苏丹驻华大使送别招待会在公共外交文化交流中心举行","uniquekey":"a3ef77503b9924d7ad6957a5af5da5d1","url":"http://mini.eastday.com/mobile/180512072229596.html"},{"author_name":"中国新闻网","category":"头条","date":"2018-05-12 07:17","thumbnail_pic_s":"http://00.imgmini.eastday.com/mobile/20180512/20180512071745_6accdc1c2a1356851f0bf51247a8f256_9_mwpm_03200403.jpg","thumbnail_pic_s02":"http://00.imgmini.eastday.com/mobile/20180512/20180512071745_6accdc1c2a1356851f0bf51247a8f256_8_mwpm_03200403.jpg","thumbnail_pic_s03":"http://00.imgmini.eastday.com/mobile/20180512/20180512071745_6accdc1c2a1356851f0bf51247a8f256_4_mwpm_03200403.jpg","title":"十九届三中全会以来推进党和国家机构改革述评","uniquekey":"b447b3941c73a35e587273a8a1b05c2d","url":"http://mini.eastday.com/mobile/180512071745564.html"},{"author_name":"阿尔法军事","category":"头条","date":"2018-05-12 07:15","thumbnail_pic_s":"http://01.imgmini.eastday.com/mobile/20180512/20180512_85e54ca8649794d2c3fdf06faf04fa87_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http://01.imgmini.eastday.com/mobile/20180512/20180512_e8df98afa3707e436d1793e66d5bc063_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http://01.imgmini.eastday.com/mobile/20180512/20180512_a1d1e3735f424eba66244a7842b2f7bf_cover_mwpm_03200403.jpg","title":"太反常!特朗普声称退出伊核协议以来,伊朗的表现让美军不适应","uniquekey":"fc91bd75121e2e84a6cb3d239ddb9846","url":"http://mini.eastday.com/mobile/180512071539046.html"},{"author_name":"东方网","category":"头条","date":"2018-05-12 07:13","thumbnail_pic_s":"http://06.imgmini.eastday.com/mobile/20180512/20180512071324_ae237ccc4fcee20ec4909588b5b7609f_1_mwpm_03200403.jpg","title":"沪正在全面消除无证餐饮店铺 \u201c共享餐厅\u201d最快6月运营","uniquekey":"ebed0b6a189873fa375a47ee8463c9be","url":"http://mini.eastday.com/mobile/180512071324017.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 07:09","thumbnail_pic_s":"http://05.imgmini.eastday.com/mobile/20180512/20180512070940_3108dd32b82c16f350b949340f95a647_1_mwpm_03200403.jpg","title":"特朗普嘲讽奥巴马:他花18亿救人,我让朝鲜免费放人","uniquekey":"8a88eb5466a46cf6d975579ccb658e29","url":"http://mini.eastday.com/mobile/180512070940666.html"},{"author_name":"新华网","category":"头条","date":"2018-05-12 07:03","thumbnail_pic_s":"http://02.imgmini.eastday.com/mobile/20180512/20180512070344_7f0366d60dea69d79c1f0432600b5f20_4_mwpm_03200403.jpg","thumbnail_pic_s02":"http://02.imgmini.eastday.com/mobile/20180512/20180512070344_7f0366d60dea69d79c1f0432600b5f20_2_mwpm_03200403.jpg","thumbnail_pic_s03":"http://02.imgmini.eastday.com/mobile/20180512/20180512070344_7f0366d60dea69d79c1f0432600b5f20_3_mwpm_03200403.jpg","title":"身在国外,这五点我必须说清楚!","uniquekey":"6113c546a13e9292ced28b0199a23108","url":"http://mini.eastday.com/mobile/180512070344831.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 06:59","thumbnail_pic_s":"http://03.imgmini.eastday.com/mobile/20180512/20180512065922_e367e5443f66512176376bd05ed65f9a_1_mwpm_03200403.jpg","title":"再\u201c读\u201d汶川|究竟是一种什么样的力量,让中国人如此勇敢地面对可怕的灾难?","uniquekey":"ca9d43ed0e6875692164bea6cc898065","url":"http://mini.eastday.com/mobile/180512065922743.html"},{"author_name":"中国网","category":"头条","date":"2018-05-12 06:50","thumbnail_pic_s":"http://03.imgmini.eastday.com/mobile/20180512/20180512065018_01abef975bd32ccc728ac4e4e6a7ec6e_2_mwpm_03200403.jpg","thumbnail_pic_s02":"http://03.imgmini.eastday.com/mobile/20180512/20180512065018_9cfcb8c2869e997daf379bb7ce19180e_6_mwpm_03200403.jpg","thumbnail_pic_s03":"http://03.imgmini.eastday.com/mobile/20180512/20180512065018_0b79422343a8c1c37ec8121cd0c7a11c_5_mwpm_03200403.jpg","title":"十年对比:通往汶川的唯一通道十年之变","uniquekey":"da0a52bc92c10e81d16a4e728701cffc","url":"http://mini.eastday.com/mobile/180512065018882.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 06:49","thumbnail_pic_s":"http://05.imgmini.eastday.com/mobile/20180512/20180512064903_6a2e203b71d2710aded08d300ab29eeb_1_mwpm_03200403.jpg","title":"全国第一!上海的马路边,应该容得下更多街头艺人","uniquekey":"fa498034a3f9cd9fa2f5e0a2fbfd4da4","url":"http://mini.eastday.com/mobile/180512064903130.html"},{"author_name":"网易越娱乐","category":"头条","date":"2018-05-12 06:46","thumbnail_pic_s":"http://04.imgmini.eastday.com/mobile/20180512/20180512064621_bab229c7b39e6fa7e59dd27703ddfb38_2_mwpm_03200403.jpg","thumbnail_pic_s02":"http://04.imgmini.eastday.com/mobile/20180512/20180512064621_bab229c7b39e6fa7e59dd27703ddfb38_3_mwpm_03200403.jpg","thumbnail_pic_s03":"http://04.imgmini.eastday.com/mobile/20180512/20180512064621_bab229c7b39e6fa7e59dd27703ddfb38_1_mwpm_03200403.jpg","title":"被吐槽演技差的宋茜这回动真格的,吴亦凡黄子韬们也跟上节奏了!","uniquekey":"e1b21b938c2fc3a7345ca5bd93ab3d8e","url":"http://mini.eastday.com/mobile/180512064621690.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 06:38","thumbnail_pic_s":"http://09.imgmini.eastday.com/mobile/20180512/20180512063843_59894e298ee0432f1d141cb347f356c8_1_mwpm_03200403.jpg","title":"美国重新制裁伊朗 德国经济界担忧遭连累","uniquekey":"526a4c110dedc726aa4e53afbcbea23d","url":"http://mini.eastday.com/mobile/180512063843828.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 06:38","thumbnail_pic_s":"http://06.imgmini.eastday.com/mobile/20180512/20180512063843_b6081361df8e6cd58b6d52035cb3ee3a_1_mwpm_03200403.jpg","title":"马哈蒂尔宣布特赦前副总理安华 二十年前送其入狱","uniquekey":"e0a21ea66ac913345a3ddeef2de9acea","url":"http://mini.eastday.com/mobile/180512063843353.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 06:38","thumbnail_pic_s":"http://03.imgmini.eastday.com/mobile/20180512/20180512063843_a198728f6df54faf3ac82f9eff70a209_1_mwpm_03200403.jpg","title":"日本二手店惊现陈水扁竞选外套 台网友酸:穿上之后手都不会抖了","uniquekey":"06c93effa61afee08c768604e97de4d2","url":"http://mini.eastday.com/mobile/180512063843086.html"},{"author_name":"人民网","category":"头条","date":"2018-05-12 06:19","thumbnail_pic_s":"http://04.imgmini.eastday.com/mobile/20180512/20180512061919_eae4ad26c428cc7f961a1d6cf6e4264e_2_mwpm_03200403.jpg","thumbnail_pic_s02":"http://04.imgmini.eastday.com/mobile/20180512/20180512061919_eae4ad26c428cc7f961a1d6cf6e4264e_7_mwpm_03200403.jpg","thumbnail_pic_s03":"http://04.imgmini.eastday.com/mobile/20180512/20180512061919_eae4ad26c428cc7f961a1d6cf6e4264e_4_mwpm_03200403.jpg","title":"10年过去了,他们还好吗","uniquekey":"ed31cf77aaf7073879898beaed6b172d","url":"http://mini.eastday.com/mobile/180512061919917.html"},{"author_name":"新闻早餐","category":"头条","date":"2018-05-12 06:15","thumbnail_pic_s":"http://02.imgmini.eastday.com/mobile/20180512/20180512_418566db5b32e600bc27d0bc8cb0a6a6_cover_mwpm_03200403.jpg","thumbnail_pic_s02":"http://02.imgmini.eastday.com/mobile/20180512/20180512_8d766fd5086f98ec8741ed44505ce35c_cover_mwpm_03200403.jpg","thumbnail_pic_s03":"http://02.imgmini.eastday.com/mobile/20180512/20180512_509df6c0c45109a6989bc86c663d02cd_cover_mwpm_03200403.jpg","title":"新闻早餐:打捞人员发现疑似杀空姐嫌犯尸体 将做DNA鉴定","uniquekey":"fa422c905b7767276acd999a9439b43c","url":"http://mini.eastday.com/mobile/180512061559294.html"},{"author_name":"大众网","category":"头条","date":"2018-05-12 06:07","thumbnail_pic_s":"http://07.imgmini.eastday.com/mobile/20180512/20180512060745_78550703b206b53ace6a0c51b1d737d4_1_mwpm_03200403.jpg","title":"难忘救援经历 回忆激励前行 ","uniquekey":"92ee0d2a31867a8f215b0517d99a9409","url":"http://mini.eastday.com/mobile/180512060745166.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 06:07","thumbnail_pic_s":"http://04.imgmini.eastday.com/mobile/20180512/20180512060707_4f667adcf6d62ec0b177add95ac037e9_1_mwpm_03200403.jpg","title":"回望汶川(下)| 房子差不多修好了,那么人呢?","uniquekey":"164f8861bd6628737684dcff49f376d2","url":"http://mini.eastday.com/mobile/180512060707642.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 06:07","thumbnail_pic_s":"http://07.imgmini.eastday.com/mobile/20180512/20180512060707_72a977a668e79cc22e88db944b2871ce_1_mwpm_03200403.jpg","title":"竟为\u201c两岸一家亲\u201d向深绿道歉,谋求连任台北市长的柯文哲向蔡英文\u201c折腰认错\u201d","uniquekey":"a8832fbcd574b6675c0cf3a0d37937bc","url":"http://mini.eastday.com/mobile/180512060707366.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 06:07","thumbnail_pic_s":"http://09.imgmini.eastday.com/mobile/20180512/20180512060706_82f81c098284dc2144d47776df34b478_1_mwpm_03200403.jpg","title":"【钩沉】\u201c摸着石头过河\u201d,是邓小平的原创吗","uniquekey":"92f7d43683b6d8b300ecb8888148d70e","url":"http://mini.eastday.com/mobile/180512060706828.html"},{"author_name":"中青在线","category":"头条","date":"2018-05-12 06:01","thumbnail_pic_s":"http://03.imgmini.eastday.com/mobile/20180512/20180512060138_731141065c8389f053e98fd09959e9b2_2_mwpm_03200403.jpg","thumbnail_pic_s02":"http://03.imgmini.eastday.com/mobile/20180512/20180512060138_731141065c8389f053e98fd09959e9b2_1_mwpm_03200403.jpg","thumbnail_pic_s03":"http://03.imgmini.eastday.com/mobile/20180512/20180512060138_731141065c8389f053e98fd09959e9b2_3_mwpm_03200403.jpg","title":"忠诚守护 老人被埋170小时 家中狗狗挣脱绳索陪伴不离","uniquekey":"4ca3a79d5762fb1d130daeadfb1f7be9","url":"http://mini.eastday.com/mobile/180512060138223.html"},{"author_name":"人民网","category":"头条","date":"2018-05-12 05:37","thumbnail_pic_s":"http://07.imgmini.eastday.com/mobile/20180512/20180512053736_8f66fb9da7fc1f943a0ce8e61a694391_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http://07.imgmini.eastday.com/mobile/20180512/20180512053736_8f66fb9da7fc1f943a0ce8e61a694391_4_mwpm_03200403.jpg","thumbnail_pic_s03":"http://07.imgmini.eastday.com/mobile/20180512/20180512053736_8f66fb9da7fc1f943a0ce8e61a694391_1_mwpm_03200403.jpg","title":"西安再添世界500强企业 将为大西安人才发展战略提供新动能","uniquekey":"e93a1d8243a9037b1da76adc7a793739","url":"http://mini.eastday.com/mobile/180512053736878.html"},{"author_name":"中国新闻网","category":"头条","date":"2018-05-12 05:36","thumbnail_pic_s":"http://02.imgmini.eastday.com/mobile/20180512/20180512053627_9945660161d740d7ed793166d7227a70_1_mwpm_03200403.jpg","thumbnail_pic_s02":"http://02.imgmini.eastday.com/mobile/20180512/20180512053627_9945660161d740d7ed793166d7227a70_2_mwpm_03200403.jpg","title":"高速车道突然飞来轮胎 司机握紧方向盘救了一车游客","uniquekey":"b006b13ae3d14707f881eb92fc914ecd","url":"http://mini.eastday.com/mobile/180512053627582.html"},{"author_name":"解放网","category":"头条","date":"2018-05-12 05:36","thumbnail_pic_s":"http://01.imgmini.eastday.com/mobile/20180512/20180512053610_4bc3792a8ae8dfa412a4da9f45478910_1_mwpm_03200403.jpg","title":"早读|上海将筹建\u201c中共一大纪念馆\u201d","uniquekey":"94eee6750d18b56e69e7f21639e3e5f1","url":"http://mini.eastday.com/mobile/180512053610949.html"},{"author_name":"西安晚报","category":"头条","date":"2018-05-12 05:23","thumbnail_pic_s":"http://02.imgmini.eastday.com/mobile/20180512/20180512052309_d81eabc5f596d49d94d73f8b3fbc6a8c_1_mwpm_03200403.jpg","title":"中国电子商务快速发展 给世界带来巨大商机","uniquekey":"fb1c5b67794834d0a6103a20b64854c0","url":"http://mini.eastday.com/mobile/180512052309026.html"},{"author_name":"大众网","category":"头条","date":"2018-05-12 05:15","thumbnail_pic_s":"http://04.imgmini.eastday.com/mobile/20180512/20180512051537_1f79912ff681c7cc4a96a6f97f967479_1_mwpm_03200403.jpg","title":"\u201c竹简\u201d寄情 感恩父母","uniquekey":"4aa30f5d94850ca14205845bc5847c5a","url":"http://mini.eastday.com/mobile/180512051537520.html"},{"author_name":"光明网","category":"头条","date":"2018-05-12 05:07","thumbnail_pic_s":"http://09.imgmini.eastday.com/mobile/20180512/20180512050734_de8eae16975f2ddf8223b7c549096c70_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http://09.imgmini.eastday.com/mobile/20180512/20180512050734_de8eae16975f2ddf8223b7c549096c70_1_mwpm_03200403.jpg","thumbnail_pic_s03":"http://09.imgmini.eastday.com/mobile/20180512/20180512050734_de8eae16975f2ddf8223b7c549096c70_2_mwpm_03200403.jpg","title":"北京海运进出口货物七成走天津","uniquekey":"e491de85803b55c5412ad0fbb2346a87","url":"http://mini.eastday.com/mobile/180512050734360.html"},{"author_name":"凤凰网","category":"头条","date":"2018-05-12 04:56","thumbnail_pic_s":"http://02.imgmini.eastday.com/mobile/20180512/20180512045657_15014004f9669c5aee78579be692dd66_1_mwpm_03200403.jpg","thumbnail_pic_s02":"http://02.imgmini.eastday.com/mobile/20180512/20180512045657_15014004f9669c5aee78579be692dd66_2_mwpm_03200403.jpg","title":"生活中诱发痔疮的五个原因不可忽略, 痔疮的治疗方法","uniquekey":"2f2995abb898f94aeacecd6537013404","url":"http://mini.eastday.com/mobile/180512045657878.html"}]
     * msg : 成功的返回
     */

    private int code;
    private String msg;
    private List<DataBean> data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * author_name : 大国军情
         * category : 头条
         * date : 2018-05-12 07:25
         * thumbnail_pic_s : http://04.imgmini.eastday.com/mobile/20180512/20180512_fe1bf1b6ec00098f9455c84dc81e6763_cover_mwpm_03200403.jpg
         * thumbnail_pic_s02 : http://04.imgmini.eastday.com/mobile/20180512/20180512_3f636fca330f2311583896aed3185196_cover_mwpm_03200403.jpg
         * thumbnail_pic_s03 : http://04.imgmini.eastday.com/mobile/20180512/20180512_793fab03dadcd417bf2d8e5e1e48a0f5_cover_mwpm_03200403.jpg
         * title : 美国被特朗普坑惨了!又有3个铁杆盟友公开唱对台戏!
         * uniquekey : 1326bf3c4505783d4a964f81b6220e9d
         * url : http://mini.eastday.com/mobile/180512072544020.html
         */

        private String author_name;
        private String category;
        private String date;
        private String thumbnail_pic_s;
        private String thumbnail_pic_s02;
        private String thumbnail_pic_s03;
        private String title;
        private String uniquekey;
        private String url;

        public String getAuthor_name() {
            return author_name;
        }

        public void setAuthor_name(String author_name) {
            this.author_name = author_name;
        }

        public String getCategory() {
            return category;
        }

        public void setCategory(String category) {
            this.category = category;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getThumbnail_pic_s() {
            return thumbnail_pic_s;
        }

        public void setThumbnail_pic_s(String thumbnail_pic_s) {
            this.thumbnail_pic_s = thumbnail_pic_s;
        }

        public String getThumbnail_pic_s02() {
            return thumbnail_pic_s02;
        }

        public void setThumbnail_pic_s02(String thumbnail_pic_s02) {
            this.thumbnail_pic_s02 = thumbnail_pic_s02;
        }

        public String getThumbnail_pic_s03() {
            return thumbnail_pic_s03;
        }

        public void setThumbnail_pic_s03(String thumbnail_pic_s03) {
            this.thumbnail_pic_s03 = thumbnail_pic_s03;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUniquekey() {
            return uniquekey;
        }

        public void setUniquekey(String uniquekey) {
            this.uniquekey = uniquekey;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

网络请求

package com.example.yinchenglong1601r20180521.model;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Sun on 2017/11/11.
 */

public class NetWorkUtils {



    public  static  String getURL(String path){
    String data="";

        try {
            URL url = new URL(path);


            HttpURLConnection ht = (HttpURLConnection) url.openConnection();

            ht.setConnectTimeout(5000);
            ht.setReadTimeout(5000);
            int responseCode = ht.getResponseCode();


            if(responseCode==200){
                InputStream inputStream = ht.getInputStream();
               /* BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer buffer=new StringBuffer();
                String con;
                while ((con=bufferedReader.readLine())!=null){
                    buffer.append(con);
                }
               String str=buffer.toString();
                return str;*/
                byte[] b=new byte[1024];
                int len=0;
                while((len=inputStream.read(b))!=-1){
                    String s = new String(b, 0, len);
                    data+=s;


                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}

清单文件中二维码需要添加的权限和图片的权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yinchenglong1601r20180521">
    <!--二维码所需权限-->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".model.App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".view.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".view.Main2Activity"></activity>
    </application>

</manifest>

//二维码所需依赖
compile 'cn.yipianfengye.android:zxing-library:2.2'

展开阅读全文

没有更多推荐了,返回首页