//点击事件的onclick
public void btn1(View view) {
// get
HttpManager.getInstance().DoGet("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1", new HttpManager.MyCallBack() {
@Override
public void success(Response response) {
try {
text1.setText(response.body().string());
}catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void fail(String msg) {
}
});
}
-------------------------------------------------------------------------------
//简单封装
public class HttpManager {
private static volatile HttpManagerhttpManager;
private OkHttpClientclient;
private Handlerhandler =new Handler();
public static HttpManagergetInstance() {
if (httpManager ==null) {
synchronized (HttpManager.class) {
if (httpManager ==null) {
httpManager =new HttpManager();
}
}
}
return httpManager;
}
private HttpManager() {
//OkHttpClient 属性
client =new OkHttpClient.Builder()
//使用自定义拦截器
.addInterceptor(new MyInterceptor())
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
//连接超时
.connectTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
//配置缓存空间KB,MB 10MB
.cache(new Cache(new File(SDCardUtils.getSDCardPathByEnvironment() +"/Download"), 10 *1024 *1024))
.build();
}
public void DoGet(String url, final MyCallBack myCallback){
client.newCall(new Request.Builder()
.url(url)
.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
if (myCallback !=null) {
myCallback.fail(e.getMessage());
}
}
});
}
@Override
public void onResponse(Call call, final Response response)throws IOException {
handler.post(new Runnable() {
@Override
public void run() {
if (myCallback !=null) {
myCallback.success(response);
}
}
});
}
});
}
public void doJson(String url, JSONObject object, final MyCallBack callBack) {
RequestBody body = RequestBody.create(MediaType.parse("application/json"), object.toString());
client.newCall(new Request.Builder().url(url)
.post(body)
.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack !=null) {
callBack.fail(e.getMessage());
}
}
});
}
@Override
public void onResponse(Call call, final Response response)throws IOException {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack !=null) {
callBack.success(response);
}
}
});
}
});
}
public CalldoFrom(String url, FormBody body, final MyCallBack callBack) {
Call call =client.newCall(new Request.Builder().url(url)
.post(body)
.build());
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack !=null) {
callBack.fail(e.getMessage());
}
}
});
}
@Override
public void onResponse(Call call, final Response response)throws IOException {
handler.post(new Runnable() {
@Override
public void run() {
if (callBack !=null) {
callBack.success(response);
}
}
});
}
});
return call;
}
public interface MyCallBack {
void success(Response response);
void fail(String msg);
}
}
-----------------------------------------------------------
不封装的正常使用
public class HttpActivityextends AppCompatActivity {
private TextViewtext1;
private Buttonbtn1;
private Buttonbtn2;
private Buttonbtn3;
private ImageViewiv1;
OkHttpClientclient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http);
initView();
initOkHttp();
}
private void initOkHttp() {
client =new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
}
private void initView() {
text1 = (TextView) findViewById(R.id.text1);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
iv1 = (ImageView) findViewById(R.id.iv1);
}
public void btn1(View view) {
// get
client.newCall(new Request.Builder()
.url("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1")
.build()).enqueue(callback);
}
public void btn2(View view) {
//Post-From
client.newCall(new Request.Builder()
.url("http://39.98.153.96:8082/api/WeekTest/login?username=1904001&pwd=123456")
.post(new FormBody.Builder()
.add("username", "1904001")
.add("pwd", "123456")
.build())
.build()).enqueue(callback);
}
public void btn3(View view) {
//Post-Json
JSONObject object =new JSONObject();
try {
object.put("username","1904001");
object.put("pwd","123456");
object.put("sex","1");
object.put("birthday","2000-02-02");
}catch (JSONException e) {
e.printStackTrace();
}
RequestBody body = RequestBody.create(MediaType.parse("application/json"),object.toString());
client.newCall(new Request.Builder()
.url("http://39.98.153.96:8082/api/WeekTest/register")
.post(body)
.build()).enqueue(callback);
}
public void btn4(View view) {
//File-Download
client.newCall(new Request.Builder()
.url("https://gank.io/images/882afc997ad84f8ab2a313f6ce0f3522")
.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response)throws IOException {
//成功 都在异步线程
runOnUiThread(new Runnable() {
@Override
public void run() {
iv1.setImageBitmap(BitmapFactory.decodeStream(response.body().byteStream()));
}
});
}
});
}
//更新UI
Callbackcallback=new Callback() {
@Override
public void onFailure(Call call, IOException e) {
LogUtils.e(e.getMessage());
}
@Override
public void onResponse(Call call, final Response response)throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
text1.setText(response.body().string());
}catch (IOException e) {
e.printStackTrace();
}
}
});
}
};
}