MD5加密*获取企业级接口

例:

 

 

依赖:

dependencies {
    compile 'com.squareup.okhttp3:okhttp:3.4.2'
    implementation 'com.android.support:recyclerview-v7:26.+'
    implementation files('libs/gson-2.3.1.jar')
}

 

网络权限:

<uses-permission android:name="android.permission.INTERNET"/>

 

 

MainActivity

/**
 * MD5加密
 * sign签名(MD5的签名)
 * timestamp时间戳
 *获取企业级接口
 */
public class MainActivity extends AppCompatActivity {
    //需要加密参数方可以请求的Json数据网址,有有效时长
    private String categoryUrl ="https://toutiao.28.com/App/Business/category_list";
    private String sign;
    private RecyclerView recyclerView;
    private Handler handler=new Handler(  ){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage( msg );
            List<ReaBean.DataBean> data = (List<ReaBean.DataBean>) msg.obj;
            recyclerView.setLayoutManager( new LinearLayoutManager( MainActivity.this, LinearLayoutManager.VERTICAL, false) );
            MyAdapter myAdapter = new MyAdapter( data, MainActivity.this );
            recyclerView.setAdapter( myAdapter );
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        recyclerView = findViewById( R.id.recycle );
    }
    public void md5date(View view){
        //获取时间戳
        long timestamp = new Date().getTime() / 1000;//获取系统的时间戳,得到当前的秒数
        String time = String.valueOf( timestamp );
        //创建集合
        HashMap<String, String> map = new HashMap<>();
        //吧时间戳添加到集合中
        map.put( "timestamp",time);
        try {
            //map集合与秘钥
            sign = SignUtils.getSignature( map, Api.PRIVATE_KEY );
            Log.d( "shr","time-------------"+time );//  timestamp时间戳

            Log.d( "shr","sign-------------"+sign );// sign签名(MD5的签名)

        } catch (IOException e) {
            e.printStackTrace();
        }
        //网络请求数据
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        //提交到表单上

        FormBody formBody = new FormBody.Builder()
                .add( "timestamp", time )//  timestamp时间戳
                .add( "sign", sign )// sign签名(MD5的签名)
                .build();

        Request request = new Request.Builder()
                .post( formBody )
                .url( categoryUrl )
                .build();

        Call call = okHttpClient.newCall( request );
        call.enqueue( new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                ReaBean reaBean = new Gson().fromJson( string, ReaBean.class );
                List<ReaBean.DataBean> data = reaBean.getData();
                Message obtain = Message.obtain();
                obtain.obj=data;
                handler.sendMessage( obtain );
                // System.out.print( "网络 "+string);
            }
        } );
    }
}

 

activity_main 

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="md5date"
        android:text="防止抓包"/>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycle">

    </android.support.v7.widget.RecyclerView>
</LinearLayout>

 

Api

public class Api {

    public static boolean isDebug=true;
    public static String testUrl="http://192.168.199.248";
    public static String lineUrl="https://toutiao.28.com";//商机头条
    public static String baseUrl(){
        return isDebug==true?lineUrl:testUrl;
    }
    public static String url=baseUrl();
    //客户端提供好的加密密钥
    public static String PRIVATE_KEY="2074509615ee2557631024d80fd7a1a2";

}

 

 

 

ReaBean

public class ReaBean {

    /**
     * status : 1
     * msg : success
     * data : [{"c_id":"332","c_name":"餐饮小吃"},{"c_id":"333","c_name":"服装鞋帽"},{"c_id":"334","c_name":"环保机械"},{"c_id":"336","c_name":"家居建材"},{"c_id":"337","c_name":"教育网络"},{"c_id":"338","c_name":"美容保健"},{"c_id":"339","c_name":"特色项目"},{"c_id":"340","c_name":"饰品礼品"}]
     * dialog : khk7cacnf5gleqeggrhbuhgp83
     */
    private int status;
    private String msg;
    private String dialog;
    private List<DataBean> data;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

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

    public String getDialog() {
        return dialog;
    }

    public void setDialog(String dialog) {
        this.dialog = dialog;
    }

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

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

    public static class DataBean {
        /**
         * c_id : 332
         * c_name : 餐饮小吃
         */

        private String c_id;
        private String c_name;

        public String getC_id() {
            return c_id;
        }

        public void setC_id(String c_id) {
            this.c_id = c_id;
        }

        public String getC_name() {
            return c_name;
        }

        public void setC_name(String c_name) {
            this.c_name = c_name;
        }
    }
}

 

 

 

 

MyAdapter 适配器类

/**
 * data;2018/7/17
 * author:沈晖然
 * RecyclerView适配器类
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    List<ReaBean.DataBean> list;
    Context context;

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

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate( context, R.layout.item, null );
        ViewHolder viewHolder = new ViewHolder( inflate );
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title1.setText( list.get( position ).getC_id() );
        holder.title2.setText( list.get( position ).getC_name() );
    }

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

   class ViewHolder extends RecyclerView.ViewHolder{

       private final TextView title1;
       private final TextView title2;

       public ViewHolder(View itemView) {
           super( itemView );
           title1 = itemView.findViewById( R.id.title1 );
           title2 = itemView.findViewById( R.id.title1 );
       }
   }
}

 

 R.layout.item

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title2"/>
</LinearLayout>

 

阅读更多
想对作者说点什么?

博主推荐

换一批

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