当前位置: 移动技术网 > IT编程>开发语言>Java > java 中OkHttp的使用方法及实例

java 中OkHttp的使用方法及实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

java  中okhttp的使用方法及实例

概述

准备研究retrofit,而它是依赖okhttp的,所以先使用一下okhttp,不深究源码,只探究使用方法。以后有机会再翻查源码。

在进行之前,首先需要2个jar包,其中一个是okhttp的jar包,github上可以下载,另一个是它的依赖包,这个很关键,没有它,项目就无法运行。

okhttp请求的2种方式

不难猜测,涉及到网络请求,那么无非2种方式,一种是使用回调,另一种则是开启子线程执行。

第一种:开启子线程执行

okhttpclient client = new okhttpclient(); 
request build = new request.builder().url(url).build(); 
try { 
<span style="white-space:pre">  </span>response execute = client.newcall(build).execute(); 
  if(execute.issuccessful()){ 
    system.out.println("wisely aaa"); 
  } else { 
    system.out.println("wisely bbb"); 
  } 
} catch (ioexception e) { 
  e.printstacktrace(); 
} 

第二种:使用回调,我个人最喜欢使用这种。(ps:觉得自己真是too young too simple!!本来以为回调的方法是在主线程,结果发现,竟然是子线程,子线程....)

okhttpclient client = new okhttpclient(); 
request build = new request.builder().url(url).build(); 
client.newcall(build).enqueue(new callback() { 
 
  @override 
  public void onresponse(response arg0) throws ioexception { 
    system.out.println("wisely  success"); 
  } 
 
  @override 
  public void onfailure(request arg0, ioexception arg1) { 
    system.out.println("wisely  failure"); 
  } 
});  

okhttp之get请求

1、获取图片

okhttpclient client = new okhttpclient(); 
     
request build = new request.builder().url(url).build(); 
client.newcall(build).enqueue(new callback() { 
       
  @override 
  public void onresponse(response response) throws ioexception { 
//   byte[] bytes = response.body().bytes(); 
    inputstream is = response.body().bytestream(); 
    options options = new bitmapfactory.options(); 
    options.insamplesize = 8; 
//   bitmap bitmap = bitmapfactory.decodebytearray(bytes, 0, bytes.length,options); 
    bitmap bitmap = bitmapfactory.decodestream(is, null, options); 
    message msg = handler.obtainmessage(); 
    msg.obj = bitmap; 
    handler.sendmessage(msg); 
  } 
     
  @override 
  public void onfailure(request arg0, ioexception arg1) { 
    system.out.println("wisely fail:"+arg1.getcause().getmessage()); 
  } 
}); 

只写了关键代码,并未写handler的相关代码。

获取网络图片有2种方式,1是获取byte数组,2是获取输入流。注意,onresponse在子线程中...

okhttp之post请求

比起get请求,post请求的分类略多。

1、首先是最常用的表单提交。

okhttpclient client = new okhttpclient(); 
 
requestbody body = new formencodingbuilder() 
    .add("username", "13363114390") 
    .add("password", "200820e3227815ed1756a6b531e7e0d2").build(); 
 
request build = new request.builder().url(url).post(body).build(); 
client.newcall(build).enqueue(new callback() { 
 
  @override 
  public void onresponse(response response) throws ioexception { 
    string lenght = response.header("content-length"); 
    system.out.println("wisely--lenght:" + lenght); 
 
    loginresponse loginresponse = new gson().fromjson(response.body().charstream(), loginresponse.class); 
    system.out.println("wisely---" + loginresponse.getmessage()); 
  } 
 
  @override 
  public void onfailure(request arg0, ioexception arg1) { 
    system.out.println("wisely-----fail"); 
  } 
}); 
string tokeid; 
  boolean result; 
 
  public boolean isresult() { 
    return result; 
  } 
 
  public void setresult(boolean result) { 
    this.result = result; 
  } 
 
  public string getmessage() { 
    return message; 
  } 
 
  public void setmessage(string message) { 
    this.message = message; 
  } 
 
  public string gettokeid() { 
    return tokeid; 
  } 
 
  public void settokeid(string tokeid) { 
    this.tokeid = tokeid; 
  } 
 
} 

上面的是一个简单的登录表单的提交,其中将返回的json数据封装到了一个bean中。除了能够获取json数据外,还能获取到各个消息头。

2、上传图片

这是我最关心的一个功能,实验证明,okhttp上传图片的功能确实强大,支持多图片上传。

private mediatype png = mediatype.parse("application/octet-stream"); 
okhttpclient client = new okhttpclient(); 
     
requestbody body = new multipartbuilder() 
    .type(multipartbuilder.form) 
    .addpart(headers.of("content-disposition","form-data; name=\"files\";filename=\"img1.jpg\""),requestbody.create(png, file1)) 
    .addpart(headers.of("content-disposition","form-data; name=\"files\";filename=\"img2.jpg\""),requestbody.create(png, file2)).build(); 
     
request request = new request.builder() 
  <span style="white-space:pre">  </span>.url(url) 
    .post(body).build(); 
client.newcall(request).enqueue(new callback() { 
       
  @override 
  public void onresponse(response response) throws ioexception { 
         
    if(response.issuccessful()){ 
      uploadpngresponse uploadpngresponse = new gson().fromjson(response.body().charstream(), uploadpngresponse.class); 
      string msg = uploadpngresponse.getmsg(); 
           
      list<string> list = uploadpngresponse.getlist(); 
      for (string string : list) { 
        system.out.println("wisely---path:"+string); 
      } 
           
    } 
  } 
       
  @override 
  public void onfailure(request arg0, ioexception arg1) { 
    system.out.println("wisely---fail--"+arg1.getcause().getmessage()); 
  } 
}); 





class uploadpngresponse{  
string msg; 
  boolean result; 
  list<string> list; 
  public string getmsg() { 
    return msg; 
  } 
  public void setmsg(string msg) { 
    this.msg = msg; 
  } 
  public boolean isresult() { 
    return result; 
  } 
  public void setresult(boolean result) { 
    this.result = result; 
  } 
  public list<string> getlist() { 
    return list; 
  } 
  public void setlist(list<string> list) { 
    this.list = list; 
  } 
} 

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网