public class okHttp { private static Handler mhandler; private final OkHttpClient ok; private static com.example.moldelmini03.okHttp okHttp; public okHttp() { ok = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .build(); mhandler = new Handler(); } public static okHttp getshu(){ if (okHttp==null){ okHttp = new okHttp(); } return okHttp; } interface fun1 { void onres(String string); } private static void onsuccess(final String string,final fun1 callback) { mhandler.post(new Runnable() { @Override public void run() { if (callback!=null) { callback.onres(string); } } }); } public void say(String url, final fun1 callback) { Request build = new Request.Builder().url(url).build(); ok.newCall(build).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { onsuccess(response.body().string(),callback); } }); } }
okHttp的工具类
最新推荐文章于 2024-09-05 15:52:51 发布