下面的get和post都必须在子线程中执行
get请求:
private OkhttpClIEnt clIEnt = new OkhttpClIEnt(); String get(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); Response response = clIEnt.newCall(request).execute(); return response.body().string(); }
@H_502_12@post请求:
private OkhttpClIEnt clIEnt = new OkhttpClIEnt();public static final MediaType JsON = MediaType.parse("application/Json; charset=utf-8"); String post(String url, String Json) throws IOException { Requestbody body = Requestbody.create(JsON, Json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = clIEnt.newCall(request).execute(); return response.body().string(); }
@H_502_12@post请求第二个参数是请求的上传的Json数据。如果没有要上传这赋值:“”
还有一种不用jar包的方式:OkhttpUtils的使用:
在github搜索Okhttp,哪个点赞多点击哪个好吧。之后拉到下面找到下面这个:将 implementation(“com.squareup.okhttp3:okhttp:4.4.0”) 复制进 build.gradle文件(dependencIEs 里面)。然后进行右上角的同步。即可是使用了。
直接上全部代码先理解着,以后再写详细点:
private static final String PATH ="http://192.168.137.1:8080/cookie_war_exploded/ShopInfoServlet" ; private static final String TAG="okhttp"; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); } //get,返回数据 public voID getRequest(VIEw vIEw){ OkhttpClIEnt okhttpClIEnt = new OkhttpClIEnt.Builder() .connectTimeout(10000, TimeUnit.MILliSECONDS) .build(); //创建连接请求内容 Request request=new Request.Builder() .get() .url(PATH) .build(); //用ClIEnt去创建请求任务 Call task=okhttpClIEnt.newCall(request); //异步任务 task.enqueue(new Callback() { @OverrIDe public voID onFailure(@NotNull Call call, @NotNull IOException e) { Log.e(TAG," onFailure:"+e.toString()); } @OverrIDe public voID onResponse(@NotNull Call call, @NotNull Response response) throws IOException { int code = response.code(); if(code== httpURLConnection.http_OK){ Log.e(TAG,"code:"+code); ResponseBody body = response.body(); Log.e(TAG,"onResponse :"+body.string()); } } }); } //post,返回数据 public voID postCommit(VIEw vIEw){ //先有CLIEnt OkhttpClIEnt clIEnt=new OkhttpClIEnt.Builder() .connectTimeout(10000,TimeUnit.MILliSECONDS) .build(); //要提交的内容 String str="在吗"; str=new Gson().toJson(str); MediaType mediaType=MediaType.parse("application/Json"); Requestbody body=Requestbody.create(str,mediaType); final Request request=new Request.Builder() .post(body) .url(PATH) .build(); Call task=clIEnt.newCall(request); task.enqueue(new Callback() { @OverrIDe public voID onFailure(@NotNull Call call, @NotNull IOException e) { Log.e(TAG,"onFailure"+e.toString()); } @OverrIDe public voID onResponse(@NotNull Call call, @NotNull Response response) throws IOException { int code = response.code(); ResponseBody responseBody = response.body(); Log.e(TAG,"onResponse"+code+"----"+responseBody.string()); } }); } //上传文件到服务器。由于我没做过服务器来接收文件。所以这里先写个代码记录下。等下次会了再回来写详细点 public voID postfile(VIEw vIEw){ OkhttpClIEnt clIEnt=new OkhttpClIEnt.Builder() .connectTimeout(10000,TimeUnit.MILliSECONDS) .build(); file file=new file("/data/data/com.example.mybitmap/files/qq.jpg"); MediaType fileType=MediaType.parse("image/png"); Requestbody fileBody=Requestbody.create(file,fileType); final Requestbody requestbody = new Multipartbody.Builder() .addFormDataPart("file",file.getname(),fileBody) .build(); Request request=new Request.Builder() .post(requestbody) .url(PATH) .build(); Call task=clIEnt.newCall(request); task.enqueue(new Callback() { @OverrIDe public voID onFailure(@NotNull Call call, @NotNull IOException e) { Log.e(TAG,"...."); } @OverrIDe public voID onResponse(@NotNull Call call, @NotNull Response response) throws IOException { Log.e(TAG,"...."); if((response.code()==httpURLConnection.http_OK)){ ResponseBody body=response.body(); if(body!=null){ String result=body.string(); Log.e(TAG,"...."); } } } }); }}
@H_502_12@点赞收藏分享文章举报啊翔仔发布了117 篇原创文章 · 获赞 1 · 访问量 6987私信 关注 总结以上是内存溢出为你收集整理的Android--OkHttp全部内容,希望文章能够帮你解决Android--OkHttp所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)