一定加入OKHTTP的依赖
compile 'com.squareup.okhttp3:okhttp:3.4.2'
public class OkHttpmanager {
//_________________________定义成员变量____________________________
private static OkHttpmanager okHttpmanager;
private static Handler mhandler;
private static OkHttpClient mcliengt;
//__________________________使用构造方法完成初始化___________________
private OkHttpmanager(){
mcliengt = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
mhandler = new Handler();
}
//__________________________使用单例模式报漏一个方法,给外部使用___________
public static OkHttpmanager getInsetance(){
if (okHttpmanager==null){
okHttpmanager=new OkHttpmanager();
}
return okHttpmanager;
}
//________________定义接口______________________
interface Func1{
void onResponse(String rsult);
}
interface Func2{
void onResponse(byte[] result);
}
interface Func3{
void onResponse(String jsonObject);
}
//处理网络请求的方法返回的是字符串
public static void onsuccessjsonstring(final String jsonVaule,final Func1 callBack){
mhandler.post(new Runnable() {
@Override
public void run() {
if (callBack!=null) {
callBack.onResponse(jsonVaule);
}
}
});
}
//请求返回字符串
public static void asynTaskJson(String url, final Func1 callBack){
Request request = new Request.Builder().url(url).build();
mcliengt.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {}
@Override
public void onResponse(Call call, Response response) throws IOException {
onsuccessjsonstring(response.body().string(),callBack);
}
});
}
//请求返回图片
private static void onSuccessJsonStringMethod(final byte[] jsonValue,final Func2 callBack){
mhandler.post(new Runnable() {
@Override
public void run() {
if(callBack!=null){
callBack.onResponse(jsonValue);
}
}
});
}
public void asyncJsonStringByURL(final String url,final Func2 callBack){
final Request request = new Request.Builder().url(url).build();
mcliengt.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
onSuccessJsonStringMethod(response.body().bytes(),callBack);
}
});
}
//请求返回Map集合
private void onSuccessMap(final String jsonValue,final Map<String , String > parms,final Func3 callBack){
mhandler.post(new Runnable() {
@Override
public void run() {
if(callBack!=null){
callBack.onResponse(jsonValue);
}
}
});
}
public void asyncJsonStringMap(final String url, final Map<String,String> map, final Func3 callBack){
FormBody.Builder builder = new FormBody.Builder();
if (map!=null&&!map.isEmpty()){
for (Map.Entry<String,String> entry:map.entrySet()) {
builder.add(entry.getKey(),entry.getValue());
}
}
FormBody build = builder.build();
final Request request = new Request.Builder().url(url).build();
mcliengt.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {}
@Override
public void onResponse(Call call, Response response) throws IOException {
onSuccessMap(response.body().string(),map,callBack);
}
});
}
}
Activity=============================================================
public class MainActivity extends AppCompatActivity {
private ImageView img;
private OkHttpmanager okHttpmanager;
private String path="http://api.tianapi.com/social/?key=1c18879fe9abf299b86ddbfec3860f03&num=10";
private String imgUrl="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1528200350&di=fa5b53c4ee13de8e68c01a243d11629e&imgtype=jpg&er=1&src=http%3A%2F%2Fpic.yesky.com%2FuploadImages%2F2016%2F300%2F45%2FG64BYPM989P1.jpg";
private String mapUrl="http://www.xieast.com/api/user/login.php";
private Button btn;
private EditText name;
private EditText pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
btn = findViewById(R.id.btn);
name = findViewById(R.id.name);
pass = findViewById(R.id.pass);
okHttpmanager = OkHttpmanager.getInsetance();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//请求返回Map集合
Map<String,String> map=new HashMap<>();
map.put("username ","13800138000");
map.put("password ","123456");
okHttpmanager.asyncJsonStringMap(mapUrl,map, new OkHttpmanager.Func3() {
@Override
public void onResponse(String jsonObject) {
Gson gson=new Gson();
DateBase dateBase = gson.fromJson(jsonObject, DateBase.class);
String msg = dateBase.getMsg();
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
});
}
});
//请求返回图片
okHttpmanager.asyncJsonStringByURL(imgUrl,new OkHttpmanager.Func2() {
@Override
public void onResponse(byte[] result) {
Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
img.setImageBitmap(bitmap);
}
});
//请求返回Json字符串
okHttpmanager.asynTaskJson(path, new OkHttpmanager.Func1() {
@Override
public void onResponse(String rsult) {
Log.i("aaa",rsult.toString());
}
});
}
}