京东分类页面

   

 1.先添加需要的依赖

compile 'com.squareup.okhttp3:okhttp:3.3.0'
compile 'com.github.bumptech.glide:glide:3.7.0'

 2.添加网络权限

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

 3.HttPConfig类

package com.bw.lenovo.my_fenlei.http;

/**
 * 接口存放类
 */
public class HttpConfig {
    //左侧列表接口
    public static String Left_url = "https://www.zhaoapi.cn/product/getCatagory";
    //右侧列表接口
    public static String right_url = "https://www.zhaoapi.cn/product/getProductCatagory";
}

 4.根据接口去解析一键解析Bean类(LeftBean)和(RightZiBean)

 5.写okHttp网络请求框架 OkHttpUtils

package com.bw.lenovo.my_fenlei.http;

import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OKHttpUtils {

    private static OKHttpUtils okHttpUtils = null;
    private OnLoadLinstener onLoadLinstener;

    private MyViewHolder myviewholder = new MyViewHolder();

    public static OKHttpUtils getInStance() {
        if (okHttpUtils == null) {
            okHttpUtils = new OKHttpUtils();
        }
        return okHttpUtils;
    }

    public void OkGet(String url) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = myviewholder.obtainMessage();
                message.what = 1;
                message.obj = e.getMessage();
                myviewholder.sendMessage(message);


            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Message message = myviewholder.obtainMessage();
                message.what = 0;
                message.obj = response.body().string();
                myviewholder.sendMessage(message);
            }
        });
    }


    public void OKPost(String url, Map<String, String> params) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyIntercepter()).build();

        FormBody.Builder builder = new FormBody.Builder();
        if (params != null) {
            Set<String> keys = params.keySet();
            for (String key : keys) {
                String value = params.get(key);
                builder.add(key, value);
            }
        }
        FormBody body = builder.build();
        Request request = new Request.Builder().url(url).post(body).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = myviewholder.obtainMessage();
                message.what = 1;
                message.obj = e.getMessage();
                myviewholder.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Message message = myviewholder.obtainMessage();
                message.what = 0;
                message.obj = response.body().string();
                myviewholder.sendMessage(message);
            }
        });

    }

    class MyViewHolder extends Handler {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    String error = (String) msg.obj;
                    onLoadLinstener.onLoadError(error);
                    break;
                case 0:
                    String json = (String) msg.obj;
                    onLoadLinstener.onLoadSucess(json);
                    break;
            }
        }
    }


    //拦截器
    class MyIntercepter implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            RequestBody body = request.body();
            //判断如果是formBody
            if (body instanceof FormBody) {
                FormBody.Builder builder = new FormBody.Builder();
                for (int i = 0; i < ((FormBody) body).size(); i++) {
                    String key = ((FormBody) body).name(i);
                    String value = ((FormBody) body).value(i);
                    builder.add(key, value);
                }
                builder.add("source", "android");
                FormBody newbody = builder.build();

                Request request1 = request.newBuilder().post(newbody).build();
                Response response = chain.proceed(request1);
                return response;
            }

            return null;
        }
    }

    public interface OnLoadLinstener {
        void onLoadSucess(String json);

        void onLoadError(String error);

    }

    public void SetOnLoadLinstener(OnLoadLinstener onLoadLinstener) {
        this.onLoadLinstener = onLoadLinstener;
    }
}

 

一、<1>mainActivity的布局

<?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="horizontal"
    tools:context=
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值