//依赖
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' compile 'org.greenrobot:eventbus:3.1.1'
//okhttp
package mvpframework.bwie.com.mygwchttp.net; import java.util.Map; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.logging.HttpLoggingInterceptor; /** * Created by 杨杰 on 2017/11/16. */ public class HttpUtils { private static volatile HttpUtils httpUtils; private final OkHttpClient client; private HttpUtils() { HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); client = new OkHttpClient.Builder() .addInterceptor(httpLoggingInterceptor) .build(); } public static HttpUtils getHttpUtils() { if (httpUtils == null) { synchronized (HttpUtils.class) { if (httpUtils == null) { httpUtils = new HttpUtils(); } } } return httpUtils; } public void doGet(String url, Callback callback) { Request build = new Request.Builder().url(url).build(); client.newCall(build).enqueue(callback); } public void doPost(String url, Map<String, String> params, Callback callback) { FormBody.Builder builder = new FormBody.Builder(); for (Map.Entry<String, String> entry : params.entrySet()) { builder.add(entry.getKey(), entry.getValue()); } FormBody formBody = builder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); client.newCall(request).enqueue(callback); } }
//api
package mvpframework.bwie.com.mygwchttp.net; public class Api { public static final String url="http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation"; }
//OnNetlistener
package mvpframework.bwie.com.mygwchttp.net; import java.io.IOError; import java.io.IOException; public interface OnNetlistener<T>{ public void onSuccess(T t); public void Error(IOException e); }
//model层
package mvpframework.bwie.com.mygwchttp.model; import android.os.Handler; import com.google.gson.Gson; import java.io.IOException; import mvpframework.bwie.com.mygwchttp.bean.Phonebean; import mvpframework.bwie.com.mygwchttp.net.Api; import mvpframework.bwie.com.mygwchttp.net.HttpUtils; import mvpframework.bwie.com.mygwchttp.net.OnNetlistener; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; public class MainModel implements IMainModel{ private Handler handler=new Handler(); @Override public void getGoods(final OnNetlistener<Phonebean> list) { HttpUtils.getHttpUtils().doGet(Api.url, new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { list.Error(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); final Phonebean phonebean = new Gson().fromJson(string, Phonebean.class); handler.post(new Runnable() { @Override public void run() { list.onSuccess(phonebean); } }); } }); } }
------------接口----------------
package mvpframework.bwie.com.mygwchttp.model; import mvpframework.bwie.com.mygwchttp.bean.Phonebean; import mvpframework.bwie.com.mygwchttp.net.OnNetlistener; public interface IMainModel { public void getGoods(OnNetlistener<Phonebean> list); }
//view层
package mvpframework.bwie.com.mygwchttp.view; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.List; import mvpframework.bwie.com.mygwchttp.R; import mvpframework.bwie.com.mygwchttp.adpter.Myadpter; import mvpframework.bwie.com.mygwchttp.bean.Phonebean; import mvpframework.bwie.com.mygwchttp.eventbus.MessageEvent; import mvpframework.bwie.com.mygwchttp.eventbus.PriceCountEvent; import mvpframework.bwie.com.mygwchttp.presnter.ModelPresent; public class MainActivity extends AppCompatActivity implements IMainActivity { private ExpandableListView elv; private CheckBox cb; private TextView count; private TextView tv_price; private List<List<Phonebean.DataBean.DatasBean>> cbl; private Myadpter myadpter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); initView(); new ModelPresent(this).getGoods(); cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myadpter.changeAllListState(cb.isChecked()); } }); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } private void initView() { elv = (ExpandableListView) findViewById(R.id.elv); cb = (CheckBox) findViewById(R.id.cb); count = (TextView) findViewById(R.id.tv_count); tv_price = (TextView) findViewById(R.id.tv_price); } @Override public void showList(List<Phonebean.DataB