package com.example.applicationlx3.net; /** * author:Created by WangZhiQiang on 2018/6/22. */ public interface APi { String DATA_URL = "http://365jia.cn/news/api3/365jia/news/headline";
}
******************************************
package com.example.applicationlx3.net; import android.os.Handler; import android.os.Looper; import java.io.IOException; import java.util.concurrent.TimeUnit; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class OkhttpUtils { private static OkhttpUtils instance; private final OkHttpClient okHttpClient; private Handler handler; private OkhttpUtils() { okHttpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .writeTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .build(); handler = new Handler(Looper.getMainLooper()); } public static OkhttpUtils getInstance(){ if (instance == null) { instance = new OkhttpUtils(); } return instance; } public void doGet(String url, final OnNetListener onNetListener){ Request builder = new Request.Builder() .url(url) .build(); okHttpClient.newCall(builder).enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { handler.post(new Runnable() { @Override public void run() { onNetListener.OnFilder(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { final String string = response.body().string(); handler.post(new Runnable() { @Override public void run() { onNetListener.OnSuccess(string); } }); } }); } }*******************************************
package com.example.applicationlx3.net; /** * author:Created by WangZhiQiang on 2018/6/22. */ public interface OnNetListener { void OnSuccess(String result); void OnFilder(Exception e); }