使用Android Studio OkHttpClient的方法?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
首先记住,使用网络的时候一定要加入权限,加入到AndroidMainfest.xml中
<uses-permission android:name="android.permission.INTERNET" />
在初次使用的时候会出现报错。cannot resolve symbol OkHttpClient
这里需要引入
implementation 'com.squareup.okhttp3:okhttp:3.0.1'
然后刷新下项目就可以了。
代码:
package com.example.administrator.testclient; import com.squareup.*; import java.io.IOException; import okhttp3.FormBody; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class BaseHttpClient { public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); // 01. 定义okhttp private final OkHttpClient client = new OkHttpClient(); public BaseHttpClient(){ //client.connectTimeoutMillis(); } /** * 发送一个表单请求 * @throws Exception */ public void SendForm() throws Exception { RequestBody formBody = new FormBody.Builder() .add("search", "Jurassic Park") .build(); Request request = new Request.Builder() .url("https://en.wikipedia.org/w/index.php") .post(formBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } /**POST 请求 * 发送一个string请求 * @throws Exception */ public void SendPostString() throws Exception { String postBody = "" + "Releases\n" + "--------\n" + "\n" + " * _1.0_ May 6, 2013\n" + " * _1.1_ June 15, 2013\n" + " * _1.2_ August 11, 2013\n"; Request request = new Request.Builder() .url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody)) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } /**POST 请求 * 发送一个From请求 * @throws Exception */ public void SendPostFrom() throws Exception { RequestBody body = new FormBody.Builder() .add("name", "sy")//添加参数体 .add("age", "18") .build(); Request request = new Request.Builder() .post(body) //请求参数 .url("http://123.207.70.54:8080/SpringMvc/hello") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); } /**Get请求 * 发送一个From请求 * @throws Exception */ public void SendGetFrom() throws Exception { Request request = new Request.Builder() .get() //请求参数 .url("http://123.207.70.54:8080/SpringMvc/hello") .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); } }
测试发现,上面的用不了,下面放一个测试通过的方法:
public void getDatasyncFactory(){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象 Request request = new Request.Builder() .url("http://123.207.70.54:8080/SpringMvc/hello")//请求接口。如果需要传参拼接到接口后面。 .build();//创建Request 对象 Response response = null; response = client.newCall(request).execute();//得到Response 对象 if (response.isSuccessful()) { Log.d("kwwl","response.code()=="+response.code()); Log.d("kwwl","response.message()=="+response.message()); Log.d("kwwl","res=="+response.body()); //此时的代码执行在子线程,修改UI的操作请使用handler跳转到UI线程。 } } catch (Exception e) { e.printStackTrace(); } } }).start(); }
返回信息:
看完上述内容,你们掌握使用Android Studio OkHttpClient的方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。