import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;
import javax.xml.transform.OutputKeys;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkttpUrl {
private static volatile OkttpUrl sOkttpUrl;
private OkHttpClient okHttpClient;
JsonBeanLunBo jsonBeanLunBo;
private Handler mHandler=new Handler();
public OkttpUrl() {
okHttpClient = new OkHttpClient();
}
public static OkttpUrl getOkttpUrl(){
if (sOkttpUrlnull){
synchronized (OkttpUrl.class){
if (nullsOkttpUrl){
sOkttpUrl = new OkttpUrl();
}
}
}
return sOkttpUrl;
}
public void getData(String url, final SViewTow sViewTow, Type type){
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.build();
Request builder = new Request.Builder().get().url(url).build();
Call call = okHttpClient.newCall(builder);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.e("lyq","请求失败");
mHandler.post(new Runnable() {
@Override
public void run() {
sViewTow.sbt(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String stringx = response.body().string();
jsonBeanLunBo = new Gson().fromJson(stringx, JsonBeanLunBo.class);
Log.e("lyq",jsonBeanLunBo.getData().get(0).getThumbnail_pic_s()+"");
mHandler.post(new Runnable() {
@Override
public void run() {
sViewTow.cgt(jsonBeanLunBo.getData());
}
});
}
});
}
}
import java.lang.reflect.Type;
public class Model {
public void modeldata(String url, SViewTow sViewTow, Type type){
OkttpUrl.getOkttpUrl().getData(url,sViewTow,type);
}
}
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
public class Prasenter {
private SView mSView;
private Model mModel;
public void attach(SView sView){
this.mSView=sView;
mModel=new Model();
}
public void jiebang(){
if (mSView!=null){
mSView=null;
}
}
public void parsentdata(String url){
Type type = new TypeToken() {}.getType();
mModel.modeldata(url, new SViewTow() {
@Override
public void cgt(Object o) {
List list=(List)o;
mSView.cg(list);
}
@Override
public void sbt(Exception e) {
mSView.sb(e);
}
},type);
}
}
package liyuanqi.bwie.com.okhttplx02;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
public class MainActivity extends AppCompatActivity implements SView> {
String wz=“http://www.xieast.com/api/news/news.php?type=top&page=1&tdsourcetag=s_pcqq_aiomsg”;
private ViewPager pagers;
private Prasenter prasenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
pagers = (ViewPager) findViewById(R.id.pagers);
prasenter = new Prasenter();
prasenter.attach(this);
prasenter.parsentdata(wz);
}
@Override
public void cg(final List list) {
pagers.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(MainActivity.this);
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),imageView);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
});
}
@Override
public void sb(Exception e) {
Toast.makeText(MainActivity.this,"没有数据",Toast.LENGTH_SHORT).show();
}
}
public interface SView {
void cg(T t);
void sb(Exception e);
}
public interface SViewTow {
void cgt(T t);
void sbt(Exception e);
}